clear all
set more off
set sortseed 1
set seed 123456

* Set working directory
cd "/Users/nolan/Dropbox/research/noncog_va/analysis/"

************************
************************
***** Prepare Data *****
************************
************************

*********************************
*** A. Import and Append Data ***
*********************************

**** 1. Import and append CAHSEE test scores ****
local counter = 1
foreach file in A_03-05 B_06-08 C_09-11 D_12-14{
	insheet using "../data/lausd_data/raw_txt/cahsee_txt_raw/CAHSEE All Attempts_`file'.csv", comma clear
	if `counter'>1{
		append using ../data/lausd_data/raw_appended_dta/cahsee_raw
	}
	compress
	saveold ../data/lausd_data/raw_appended_dta/cahsee_raw.dta, replace
	local ++counter
}


**** 2. Import and append CELDT test scores ****
forvalues YEAR = 2003/2014 {
	insheet using "../data/lausd_data/raw_txt/celdt_txt_raw/Celdt (`YEAR').txt", tab clear
	tostring celdtproflvlcode, replace
	if `YEAR'>2003{
		append using "../data/lausd_data/raw_appended_dta/celdt_raw.dta"
	}
	compress
	saveold "../data/lausd_data/raw_appended_dta/celdt_raw.dta", replace
}


**** 3. Import and append CST test scores ****
forvalues year=2003/2014{
	local counter_`year' = 1
}

local files: dir "../data/lausd_data/raw_txt/cst_txt_raw/" files "*.txt"

foreach file in `files'{
	di "`file'"
	local year = substr("`file'",6,4)
	insheet using "../data/lausd_data/raw_txt/cst_txt_raw/`file'", tab clear
	tostring castandardproflevelcode, replace
	if `counter_`year''>1{
		append using "../data/lausd_data/raw_appended_dta/cst_`year'_raw.dta"
	}
	local ++counter_`year'
	compress
	saveold "../data/lausd_data/raw_appended_dta/cst_`year'_raw.dta", replace
}



**** 4. Import and append elementary school marks ****
forvalues year=2003/2014{
	local counter_`year' = 1
}

local files: dir "../data/lausd_data/raw_txt/elem_marks_txt_raw/" files "*.txt"

foreach file in `files'{
	di "`file'"
	local year = substr("`file'",19,4)
	insheet using "../data/lausd_data/raw_txt/elem_marks_txt_raw/`file'", tab clear
	tostring gradecode, replace
	destring markforachievement, ignore("~") replace
	destring markforworkeffort, ignore("~")  replace
	if `counter_`year''>1{
		append using "../data/lausd_data/raw_appended_dta/elem_marks_`year'_raw.dta"
	}
	local ++counter_`year'
	compress
	saveold "../data/lausd_data/raw_appended_dta/elem_marks_`year'_raw.dta", replace
}



**** 5. Import and append secondary school marks ****
forvalues year=2003/2014{
	local counter_`year' = 1
}

local files: dir "../data/lausd_data/raw_txt/second_marks_txt_raw/" files "*.txt"

foreach file in `files'{
	di "`file'"
	local year = substr("`file'",18,4)
	insheet using "../data/lausd_data/raw_txt/second_marks_txt_raw/`file'", tab clear
	if `counter_`year''>1{
		append using "../data/lausd_data/raw_appended_dta/second_marks_`year'_raw.dta"
	}
	local ++counter_`year'
	compress
	saveold "../data/lausd_data/raw_appended_dta/second_marks_`year'_raw.dta", replace
}


**** 6. Demographics ****
insheet using "../data/lausd_data/raw_txt/demographics_txt_raw/Demographics.txt", tab clear
saveold "../data/lausd_data/raw_appended_dta/demographics_raw.dta", replace


**** 7. Elementary School Attendance ****
insheet using "../data/lausd_data/raw_txt/elem_attendance_txt_raw/Elementary Attendance (2004-2009).txt", tab clear
saveold "../data/lausd_data/raw_appended_dta/elem_attendance_raw.dta", replace


**** 8. PSAT ****
insheet using "../data/lausd_data/raw_txt/psat_txt_raw/Psat 2003-2014.txt", tab clear
saveold "../data/lausd_data/raw_appended_dta/psat_raw.dta", replace


**** 9. SAT ****
insheet using "../data/lausd_data/raw_txt/sat_txt_raw/LAUSD_SAT_TEST_Pseudo.txt", comma clear
saveold "../data/lausd_data/raw_appended_dta/sat_raw.dta", replace


**** 10. School Completion ****
insheet using "../data/lausd_data/raw_txt/school_completion_txt_raw/LAUSD_STU_INFO_Pseudo.txt", comma  clear
saveold "../data/lausd_data/raw_appended_dta/school_completion_raw.dta", replace


**** 11. Suspensions ****
insheet using "../data/lausd_data/raw_txt/suspensions_txt_raw/Suspensions 2003-2014.txt", tab clear
saveold "../data/lausd_data/raw_appended_dta/suspensions_raw.dta", replace


**** 12. Year to Date Attendance ****
insheet using "../data/lausd_data/raw_txt/ytd_attendance_txt_raw/Ytd Attendance (2010-2014).txt", tab clear
saveold "../data/lausd_data/raw_appended_dta/ytd_attendance_raw.dta", replace


**** 13. Import and append secondary school attendance ****
local files: dir "../data/lausd_data/raw_txt/second_attendance_txt_raw/" files "*.txt"
local counter=1
foreach file in `files'{
	di "`file'"
	local year = substr("`file'",strpos("`file'","2"),4)
	insheet using "../data/lausd_data/raw_txt/second_attendance_txt_raw/`file'", tab clear
	if `counter'>1{
		append using "../data/lausd_data/raw_appended_dta/second_attendance_raw.dta"
	}
	local ++counter
	compress
	saveold "../data/lausd_data/raw_appended_dta/second_attendance_raw.dta", replace
}


**** 14. Work habit marks ****
forvalues x = 2006/2014 {
	insheet using "../data/lausd_data/raw_txt/workhabits_txt_raw/`x'.txt", comma clear
	
	rename e107517 grade 
	rename e109725 semester
	rename e199812 stdpseudoid 
	rename e278053 workhabit 
	rename e278064 schendyr
	rename e278090 mark 
	rename as102277_107517_old gradecode
	
	saveold "../data/lausd_data/raw_appended_dta/workhabits_`x'_raw.dta", replace
}


***************************************************
*** A.2 Import and Append Additional LAUSD Data ***
***************************************************
foreach file in q1A_Grads1415 q1B_Grads1314 q2A_SAT_1314 q2B_SAT_1415 q3_ACT q4_PSAT q6_YTDAttend q7_Suspensions {
	di "`file'"
	import excel "../data/lausd_data/raw_txt/added_data_june2016_raw/`file'.xlsx", firstrow clear
	foreach var of varlist * {
		rename `var' `=lower("`var'")'
	}
	destring, replace
	format studentpseudoid %13.0g
	
	compress
	saveold "../data/lausd_data/temp/`file'.dta", replace
}

foreach file in q5A_Gr6-8_Fall15 q5B_Gr9-12_Fall15 q5C_Gr6-8_Spring15 q5D_Gr9-12_Spring15 {
	di "`file'"
	insheet using "../data/lausd_data/raw_txt/added_data_june2016_raw/`file'.txt", comma clear
	foreach var of varlist * {
		rename `var' `=lower("`var'")'
	}
	destring, replace
	format stdpseudoid %13.0g
	
	compress
	saveold "../data/lausd_data/temp/`file'.dta", replace
}


*** Graduated 2014-15
use "../data/lausd_data/temp/q1A_Grads1415.dta", clear 

rename studentpseudoid stdpseudoid
rename school_code cdscode
rename gendercode gender 
rename completionreason graduated

replace graduated = "Not Graduated" if graduated == "Completer"
replace graduated = "NA" if graduated == "N/A"

save "../data/lausd_data/temp/q1A_Grads1415.dta", replace

*** Graduated 2013-14
use "../data/lausd_data/temp/q1B_Grads1314.dta", clear 

rename studentpseudoid stdpseudoid
rename school_code cdscode
rename gendercode gender 
rename completionreason graduated

replace graduated = "Not Graduated" if graduated == "Completer"
replace graduated = "NA" if graduated == "N/A"

replace grade = "" if grade == "US"
destring, replace

save "../data/lausd_data/temp/q1B_Grads1314.dta", replace

*** SAT 2013-14
use "../data/lausd_data/temp/q2A_SAT_1314.dta", clear 

rename studentpseudoid stdpseudoid
rename testdate date 
rename subm math
rename subv reading 
rename write writing 

rename mathnatlile mathile
rename vocnatlile readile
rename writnatlile writile

gen tooksat = "Yes"
gen total = math + reading + writing

tostring math reading writing total, replace

save "../data/lausd_data/temp/q2A_SAT_1314.dta", replace

*** SAT 2014-15
use "../data/lausd_data/temp/q2B_SAT_1415.dta", clear 

rename studentpseudoid stdpseudoid
rename testdate date 
rename subm math
rename subv reading 
rename write writing 

rename mathnatlile mathile
rename vocnatlile readile
rename writnatlile writile

gen tooksat = "Yes"
gen total = math + reading + writing

tostring math reading writing total, replace

save "../data/lausd_data/temp/q2B_SAT_1415.dta", replace


*** ACT 
use "../data/lausd_data/temp/q3_ACT.dta", clear 

rename studentpseudoid stdpseudoid
rename schoolyear year
rename math actmath 
rename english actenglish
rename reading actreading
rename sciencereasoning actscience
rename composite actcomposite
rename writingsubscore actwriting
rename testdatemmyy actdate

replace actwriting = "02" if actwriting == "--"
replace year = substr(year,-2,2)
destring, replace 
replace year = 2000 + year

save "../data/lausd_data/raw_appended_dta/act_raw.dta", replace 

*** PSAT 2014-15
use "../data/lausd_data/temp/q4_PSAT.dta", clear 

rename studentpseudoid stdpseudoid
rename year schendyr 
rename rawmth psatscoremath 
rename rawread psatscoreread 
rename rawwrite psatscorewrite 
rename rawindex psatscoreoverall
rename mathile psatpctmath 
rename readile psatpctread 
rename writeile psatpctwrite 
rename indexile psatpctoverall

save "../data/lausd_data/temp/q4_PSAT.dta", replace


*** Attendance 2015
use "../data/lausd_data/temp/q6_YTDAttend.dta", clear 

rename endyear schendyr
rename ytdofenrolleddays ytdenrolleddaycnt 
rename ytdofattendeddays ytdattendeddaycnt 
rename ytdofabsentdays ytdabsentdaycnt 
rename studentpseudoid stdpseudoid 
rename schoollocationcode schlocationcode 
rename schoollocationname schschoolname 
rename cdecdsschoolcode cdscode 
rename esccode eduservctrcode

save "../data/lausd_data/temp/q6_YTDAttend.dta", replace 


*** Suspensions 2015
use "../data/lausd_data/temp/q7_Suspensions.dta", clear 

rename endyear schendyr
rename esccode eduservctrcode 
rename suspensiontypedescription suspensiontypedescr 
rename studentpseudoid stdpseudoid 
rename ofsuspendeddays suspensiondayscnt 
rename schoollocationcode schlocationcode 
rename schoollocationname schschoolname

save "../data/lausd_data/temp/q7_Suspensions.dta", replace 

*** Secondary Grades 2015
foreach var in q5A_Gr6-8_Fall15 q5B_Gr9-12_Fall15 q5C_Gr6-8_Spring15 q5D_Gr9-12_Spring15 { 
	use "../data/lausd_data/temp/`var'.dta", clear 

	rename prlpreferredlocationcode schlocationcode
	rename prlpreferredlocationname schschoolname
	rename transgradecode gradecode
	gen crsapcourseflag = "N"
	replace crsapcourseflag = "Y" if substr(crscoursename,1,3) == "AP "

	drop cdecdsschoolcode dptdepartmentname classperiodcode gender stdgradefirstenroll stdca1stschooldate cdecdsschoolcode

	save "../data/lausd_data/temp/`var'.dta", replace 
}

**** 5. Import and append secondary school marks for 2015 year ****
use "../data/lausd_data/temp/q5A_Gr6-8_Fall15.dta", clear
	
foreach var in q5B_Gr9-12_Fall15 q5C_Gr6-8_Spring15 q5D_Gr9-12_Spring15 { 
	append using "../data/lausd_data/temp/`var'.dta"
}

saveold "../data/lausd_data/raw_appended_dta/second_marks_2015_raw.dta", replace

**** 9. SAT for 2014 and 2015 year ****
use "../data/lausd_data/raw_appended_dta/sat_raw.dta", clear

append using "../data/lausd_data/temp/q2A_SAT_1314.dta"
append using "../data/lausd_data/temp/q2B_SAT_1415.dta"

saveold "../data/lausd_data/raw_appended_dta/sat_raw.dta", replace

**** 10. School Completion for 2014 and 2015 year ****
use "../data/lausd_data/raw_appended_dta/school_completion_raw.dta", clear

append using "../data/lausd_data/temp/q1A_Grads1415.dta"
append using "../data/lausd_data/temp/q1B_Grads1314.dta"

saveold "../data/lausd_data/raw_appended_dta/school_completion_raw.dta", replace

**** 11. Suspensions for 2015 year ****
use "../data/lausd_data/raw_appended_dta/suspensions_raw.dta", clear

append using "../data/lausd_data/temp/q7_Suspensions.dta"

saveold "../data/lausd_data/raw_appended_dta/suspensions_raw.dta", replace

**** 12. Year to Date Attendance for 2015 year ****
use "../data/lausd_data/raw_appended_dta/ytd_attendance_raw.dta", clear

append using "../data/lausd_data/temp/q6_YTDAttend.dta"

saveold "../data/lausd_data/raw_appended_dta/ytd_attendance_raw.dta", replace

*************************************************
*** B. Clean, Rehape Data, and Prep for Merge ***
*************************************************

**** 1. CAHSEE test scores **** 
use ../data/lausd_data/raw_appended_dta/cahsee_raw.dta, clear
keep if testtotalflag == "Y"
drop if scaledscoreamount == 0 

keep endyear studentpseudoid attemptquantity testgroupname passindicatorcode scaledscoreamount
rename endyear schendyr
rename studentpseudoid stdpseudoid
rename scaledscoreamount cahseescore 

replace testgroupname = "math" if testgroupname == "MATH"
replace testgroupname = "ela" if testgroupname == "ELA"
 
gen cahseepassed = (passindicatorcode == "Y")
drop passindicatorcode

replace cahseescore = 450 if cahseescore > 450 & cahseescore < 999999

egen cahseeattempts = max(attemptquantity), by(stdpseudoid testgroupname)

keep if attemptquantity == 1 
drop attemptquantity

sort stdpseudoid testgroupname cahseepassed
drop if stdpseudoid == stdpseudoid[_n-1] & testgroupname == testgroupname[_n-1] & cahseepassed == 1

reshape wide cahseescore cahseepassed cahseeattempts, i(stdpseudoid schendyr) j(testgroupname) string

compress
saveold "../data/lausd_data/raw_reshaped_dta/cahsee_reshaped.dta", replace


**** 2. CELDT Test Scores ****
use "../data/lausd_data/raw_appended_dta/celdt_raw.dta", clear
sort stdpseudoid schendyr tsttestname scaledscoreamt
drop celdtproflvlcode celdtproflvldescr
drop if stdpseudoid == stdpseudoid[_n-1] & tsttestname == tsttestname[_n-1] & schendyr == schendyr[_n-1]
reshape wide scaledscoreamt, i(stdpseudoid schendyr) j(tsttestname) string

rename scaledscoreamtLISTENING celdtlistening
rename scaledscoreamtLIS_SPEAK celdtlis_speak
rename scaledscoreamtOVERALL celdtoverall
rename scaledscoreamtREADING celdtreading
rename scaledscoreamtSPEAKING celdtspeaking
rename scaledscoreamtWRITING celdtwriting
rename celdttesttypedescr celdttype
rename gradecode gradecode_celdt
rename schlocationcode schlocationcode_celdt
rename schschoolname schschoolname_celdt
rename eduservctrcode eduservctrcode_celdt
rename cdscode cdscode_celdt 

replace celdtlis_speak = (celdtlistening + celdtspeaking)/2 if celdtlis_speak == . 

foreach var in celdtlis_speak celdtoverall celdtreading celdtwriting { 
	egen mean`var' = mean(`var'), by(schendyr)
	egen sd`var' = sd(`var'), by(schendyr)
	gen z`var' = (`var' - mean`var')/sd`var'
	drop mean`var' sd`var'
}

keep schendyr stdpseudoid celdtlis_speak celdtoverall celdtreading ///
	celdtwriting celdttype gradecode_celdt schlocationcode_celdt schschoolname_celdt cdscode_celdt eduservctrcode_celdt ///
	zceldtlis_speak zceldtoverall zceldtreading zceldtwriting

compress
saveold "../data/lausd_data/raw_reshaped_dta/celdt_raw_reshaped.dta", replace


**** 3. CST Test Scores ****
forvalues z = 2003/2014 {
	use "../data/lausd_data/raw_appended_dta/cst_`z'_raw.dta", clear
	keep if tsttotalflag == "Y"
	replace tstgroupname = lower(tstgroupname)
	replace tstgroupname  = "socialsci" if tstgroupname == "social science"	
	
	replace tstgroupname = "social8" if tsttestname == "HISTORY-SOC SCI (GRAD" | tsttestname == "HISTORY-SOC SCI (GRADE 8)"
	replace tstgroupname = "social11" if tsttestname == "US HISTORY"
	replace tstgroupname = "socialworld" if tsttestname == "WORLD HISTORY"
	
	replace tstgroupname = "science5" if tsttestname == "SCIENCE (GRADE 5)"
	replace tstgroupname = "science8" if tsttestname == "SCIENCE (GRADE 8)"
	replace tstgroupname = "science10" if tsttestname == "LIFE SCIENCE"
	
	drop if tstgroupname == "science" | tstgroupname == "writing"
	
	foreach var in gradecode schlocationcode schschoolname cdscode eduservctrcode {
		egen `var'_cst = mode(`var'), by(schendyr stdpseudoid) minmode
	}	

	keep schendyr cstscaledscoreamt tstgroupname stdpseudoid gradecode_cst schlocationcode_cst ///
		schschoolname_cst cdscode_cst eduservctrcode_cst

	sort stdpseudoid schendyr tstgroupname cstscaledscoreamt
	drop if stdpseudoid == stdpseudoid[_n-1] & tstgroupname == tstgroupname[_n-1] & schendyr == schendyr[_n-1]
	
	rename cstscaledscoreamt cstscore 
	
	replace cstscore = . if cstscore == 0
	
	egen mean = mean(cstscore), by(gradecode_cst tstgroupname) 
	egen sd   = sd(cstscore), by(gradecode_cst tstgroupname)
	gen  zcst = (cstscore - mean)/sd
	drop mean sd
		
	* Long Version
	*compress
	*saveold "../data/lausd_data/raw_long_dta/cst_`z'_raw_long.dta", replace
	
	reshape wide cstscore zcst, i(stdpseudoid schendyr) j(tstgroupname) string

	* Wide Version
	compress
	saveold "../data/lausd_data/raw_reshaped_dta/cst_`z'_raw_reshaped.dta", replace
}


**** 4. Elementary Marks ****
forvalues z = 2003(1)2014 {
	use "../data/lausd_data/raw_appended_dta/elem_marks_`z'_raw.dta", clear

	sort stdpseudoid schendyr crscoursename markingperiodcode
	duplicates tag stdpseudoid schendyr crscoursename markingperiodcode, gen(t)
	assert t==0
	drop t
	drop if stdpseudoid == stdpseudoid[_n-1] & crscoursename == crscoursename[_n-1] & ///
		schendyr == schendyr[_n-1] & markingperiodcode == markingperiodcode[_n-1]

	foreach var in gradecode schlocationcode schschoolname eduservctrcode {
		egen `var'_elemmarks = mode(`var'), by(schendyr stdpseudoid) minmode
	}	
	
	keep schendyr crscoursename markforachievement markforworkeffort tchpseudoid stdpseudoid markingperiodcode ///
	gradecode_elemmarks schlocationcode_elemmarks schschoolname_elemmarks eduservctrcode_elemmarks

	replace crscoursename = "art" if crscoursename == "ARTS"
	replace crscoursename = "eldread" if crscoursename == "ELD READING"
	replace crscoursename = "eldspeak" if crscoursename == "ELD SPEAKING"
	replace crscoursename = "eldwrite" if crscoursename == "ELD WRITING"
	replace crscoursename = "eldlisten" if crscoursename == "ELD LISTENING"
	replace crscoursename = "health" if crscoursename == "HEALTH ED"
	replace crscoursename = "hist" if crscoursename == "HIST SOC SCI"
	replace crscoursename = "listen" if crscoursename == "LISTENING"
	replace crscoursename = "math" if crscoursename == "MATHEMATICS"
	replace crscoursename = "pe" if crscoursename == "PHYSICAL ED"
	replace crscoursename = "read" if crscoursename == "READING"
	replace crscoursename = "science" if crscoursename == "SCIENCE"
	replace crscoursename = "speak" if crscoursename == "SPEAKING"
	replace crscoursename = "write" if crscoursename == "WRITING"

	rename markforachievement achmark
	rename markforworkeffort effmark
	egen tchpseudoidfrommarks = mode(tchpseudoid), by(stdpseudoid) minmode
	gen cool = (tchpseudoid == tchpseudoidfrommarks)
	tab cool 
	drop tchpseudoid cool
    replace markingperiodcode = lower(markingperiodcode)
	
	*saveold "../data/lausd_data/raw_long_dta/elemmarks_`z'_raw_long.dta", replace	
	
	reshape wide achmark effmark, i(stdpseudoid schendyr markingperiodcode) j(crscoursename) string
	
	reshape wide achmarkart-effmarkwrite, i(stdpseudoid schendyr) j(markingperiodcode) string
	
	compress
	saveold "../data/lausd_data/raw_reshaped_dta/elemmarks_`z'_raw_reshaped.dta", replace
}


**** 5. Secondary Marks ****
* Create List of Math & English Classes by Grade
forvalues z = 2003(1)2015 {
	use crscoursecode crscoursename crscoursecode gradecode using "../data/lausd_data/raw_appended_dta/second_marks_`z'_raw.dta", clear

	gen crscodenumeric = substr(crscoursecode,1,6)
	replace crscodenumeric = "0" if crscodenumeric == "UNKNOW" 
	destring crscodenumeric, replace
	drop crscoursecode
	gen crstype = "english" if (crscodenumeric >= 230000 & crscodenumeric <= 239999) /// 
		| (crscodenumeric >= 170000 & crscodenumeric <= 172400)
	replace crstype = "math" if crscodenumeric >= 310000 & crscodenumeric <= 319999
	drop if crstype==""
	duplicates drop
	if `z'>2003{
		append using "../data/lausd_data/temp/math_english_list.dta"
	}
	duplicates drop
	sort crstype gradecode crscodenumeric crscoursename
	compress
	save "../data/lausd_data/temp/math_english_list.dta", replace
}
* Import coded list of math and English Classes
import excel using ./input/math_ela_track.xlsx, clear firstrow 
save ../data/lausd_data/temp/tracking, replace

*** 2003 to 2015
forvalues z = 2003(1)2015 {
	
	use "../data/lausd_data/raw_appended_dta/second_marks_`z'_raw.dta", clear
	
	if `z'!=2015{
		sort schendyr stdpseudoid tchpseudoid crscoursecode occoccurrencename markforachievement markforworkeffort markforcooperation
		
	}
	if `z'==2015{
		sort schendyr stdpseudoid tchpseudoid crscoursecode occoccurrencename markforachievement 
	}
	
	drop if stdpseudoid == stdpseudoid[_n-1] & schendyr == schendyr[_n-1] ///
	  & tchpseudoid == tchpseudoid[_n-1] ///
	  & occoccurrencename == occoccurrencename[_n-1] & crscoursecode == crscoursecode[_n-1]		

	tab markforachievement
	replace markforachievement = "" if inlist(markforachievement,"A","B","C","D","F")==0 
	
	replace markforachievement = "4" if markforachievement == "A"
	replace markforachievement = "3" if markforachievement == "B"
	replace markforachievement = "2" if markforachievement == "C"
	replace markforachievement = "1" if markforachievement == "D"
	replace markforachievement = "0" if markforachievement == "F"

	if `z'!=2015{
		tab markforworkeffort
		tab markforcooperation
		
		replace markforworkeffort = "" if inlist(markforworkeffort,"E","S","U")==0
		replace markforcooperation = "" if inlist(markforcooperation,"E","S","U")==0
		
		replace markforworkeffort = "3" if markforworkeffort == "E"
		replace markforworkeffort = "2" if markforworkeffort == "S"
		replace markforworkeffort = "1" if markforworkeffort == "U"
		
		replace markforcooperation = "3" if markforcooperation == "E"
		replace markforcooperation = "2" if markforcooperation == "S"
		replace markforcooperation = "1" if markforcooperation == "U"
	}
	
	gen ap = (crsapcourseflag == "Y")
	egen apsemesters = total(ap), by(schendyr stdpseudoid)

	gen crscodenumeric = substr(crscoursecode,1,6)
	replace crscodenumeric = "0" if crscodenumeric == "UNKNOW" 
	destring crscodenumeric, replace

	gen crstype = ""
	replace crstype = "social" if crscodenumeric >= 370000 & crscodenumeric <= 379999
	replace crstype = "english" if (crscodenumeric >= 230000 & crscodenumeric <= 239999) | (crscodenumeric >= 170000 & crscodenumeric <= 172400)
	replace crstype = "math" if crscodenumeric >= 310000 & crscodenumeric <= 319999
	replace crstype = "science" if crscodenumeric >= 360000 & crscodenumeric <= 369999
	replace crstype = "lang" if crscodenumeric >= 250000 & crscodenumeric <= 259999
	replace crstype = "pe" if crscodenumeric >= 330000 & crscodenumeric <= 339999
	replace crstype = "health" if crscodenumeric >= 260000 & crscodenumeric <= 269999
	replace crstype = "art" if crscodenumeric >= 200000 & crscodenumeric <= 209999
	replace crstype = "other" if crstype == ""

	merge m:1 crstype crscodenumeric crscoursename gradecode using "../data/lausd_data/temp/tracking", keep(1 3)
	ren track mathtrack
	ren honors math_ela_honors
	ren electivehelper ela_noncore
	
	forvalues grade=6/12{
		di "grade `grade'"
		tab crscoursename if grade==`grade' & crstype=="math"
	}
		
	assert occoccurrencename=="FALL" | occoccurrencename=="SPRING"
	* Class definition is teacher-semester-course id
	egen classid = group(crscoursecode tchpseudoid occoccurrencename)

	if `z' != 2015{
		destring markforachievement markforworkeffort markforcooperation, replace
		keep schendyr stdpseudoid crstype crstype ap apsemesters gradecode ///
			markforachievement markforworkeffort markforcooperation ///
			schlocationcode schschoolname /// 
			eduservctrcode tchpseudoid classid mathtrack math_ela_honors ela_noncore
	}
	if `z' == 2015{
		destring markforachievement , replace
		keep schendyr stdpseudoid crstype ap apsemesters gradecode ///
			markforachievement schlocationcode schschoolname /// 
			tchpseudoid classid mathtrack math_ela_honors ela_noncore
	}
	gen apmath_classes = ap*(crstype=="math")
	gen trackmath_a = mathtrack=="a"
	gen trackmath_b = mathtrack=="b"
	gen trackmath_c = mathtrack=="c"
	gen trackmath_r = mathtrack=="r"
	gen apenglish_classes = ap*(crstype=="english")
	gen ela_elective = ela_noncore=="e"
	gen ela_helper = ela_noncore=="r"
	
	compress
	saveold "../data/lausd_data/raw_long_dta/second_marks_`z'_raw_long.dta", replace
	

	egen gpatotal = mean(markforachievement), by(schendyr stdpseudoid)
	egen classcount_gpa = count(markforachievement), by(schendyr stdpseudoid)

	
	if `z'!=2015{
		egen workeffortgpatotal = mean(markforworkeffort), by(schendyr stdpseudoid)
		egen coopgpatotal = mean(markforcooperation), by(schendyr stdpseudoid)
		egen classcount_effort= count(markforworkeffort), by(schendyr stdpseudoid)
		egen classcount_coop = count(markforcooperation), by(schendyr stdpseudoid)
		egen workeffortgpa = mean(markforworkeffort), by(schendyr stdpseudoid crstype)
		egen coopgpa = mean(markforcooperation), by(schendyr stdpseudoid crstype)
	}
	
	foreach var in gradecode schlocationcode schschoolname  {
		egen `var'_secondmarks = mode(`var'), by(schendyr stdpseudoid) minmode
	}
	if `z'!=2015{
		egen eduservctrcode_secondmarks = mode(eduservctrcode), by(schendyr stdpseudoid) minmode
	}
	foreach var in apmath_classes trackmath_a trackmath_b trackmath_c /// 
				trackmath_r apenglish_classes ela_elective ela_helper{
		egen t_`var' = total(`var'), by(schendyr stdpseudoid)
		drop `var'
		ren t_`var' `var'
	}
	
	if `z' != 2015{
	  keep schendyr stdpseudoid crstype gpatotal workeffortgpatotal coopgpatotal gpa workeffortgpa ///
		coopgpa apsemesters gradecode_secondmarks schlocationcode_secondmarks schschoolname_secondmarks /// 
		eduservctrcode_secondmarks classcount_gpa classcount_effort classcount_coop ///
		apmath_classes trackmath_a trackmath_b trackmath_c trackmath_r apenglish_classes ///
		ela_elective ela_helper
		
	  sort stdpseudoid crstype
	  drop if stdpseudoid == stdpseudoid[_n-1] & crstype == crstype[_n-1] 	
	
	  reshape wide gpa workeffortgpa coopgpa, i(stdpseudoid schendyr apmath_classes trackmath_a trackmath_b trackmath_c trackmath_r apenglish_classes ela_elective ela_helper) j(crstype) string
	}
	if `z' == 2015{
		keep schendyr stdpseudoid crstype gpatotal gpa apsemesters ///
			gradecode_secondmarks schlocationcode_secondmarks schschoolname_secondmarks classcount_gpa ///
			apmath_classes trackmath_a trackmath_b trackmath_c trackmath_r apenglish_classes ///
			ela_elective ela_helper	
		
	  sort stdpseudoid crstype
	  drop if stdpseudoid == stdpseudoid[_n-1] & crstype == crstype[_n-1] 	
	
	  reshape wide gpa, i(stdpseudoid schendyr apmath_classes trackmath_a trackmath_b trackmath_c trackmath_r apenglish_classes ela_elective ela_helper) j(crstype) string

	}
	compress
	saveold "../data/lausd_data/raw_reshaped_dta/second_marks_`z'_raw_reshaped.dta", replace	
	
}

**** 6. Demographics ****
use "../data/lausd_data/raw_appended_dta/demographics_raw.dta", clear
sort schendyr stdpseudoid schlocationcode
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1]

compress
saveold "../data/lausd_data/raw_reshaped_dta/demographics_raw_reshaped.dta", replace


**** 7. Attendance ****
*** Attendance from 2010 to 2015 for all students 
use "../data/lausd_data/raw_appended_dta/ytd_attendance_raw.dta", clear

sort schendyr stdpseudoid schlocationcode
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1]

foreach var in gradecode schlocationcode schschoolname cdscode eduservctrcode {
	rename `var' `var'_att 
}	

keep schendyr ytdenrolleddaycnt ytdattendeddaycnt ytdabsentdaycnt stdpseudoid ///
	gradecode_att schlocationcode_att schschoolname_att cdscode_att eduservctrcode_att

compress
saveold "../data/lausd_data/raw_reshaped_dta/attendance_2010_2014_all.dta", replace

*** Attendance from 2004 to 2009 for elementary students
use "../data/lausd_data/raw_appended_dta/elem_attendance_raw.dta", clear
sort schendyr stdpseudoid occoccurrencename

egen ytdattendeddaycnt = total(presentdaysqty), by(schendyr stdpseudoid)
egen ytdabsentdaycnt = total(absentdaysqty), by(schendyr stdpseudoid)

foreach var in gradecode schlocationcode schschoolname cdscode eduservctrcode {
	egen `var'_att = mode(`var'), by(schendyr stdpseudoid) minmode
}	

sort schendyr stdpseudoid occoccurrencename
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1] 

gen ytdenrolleddaycnt = ytdattendeddaycnt + ytdabsentdaycnt

keep schendyr ytdenrolleddaycnt ytdattendeddaycnt ytdabsentdaycnt stdpseudoid ///
	gradecode_att schlocationcode_att schschoolname_att cdscode_att eduservctrcode_att

compress
saveold "../data/lausd_data/raw_reshaped_dta/attendance_2004_2009_elem.dta", replace

*** Attendance from 2003 to 2009 for secondary students
use "../data/lausd_data/raw_appended_dta/second_attendance_raw.dta", clear

sort schendyr stdpseudoid occoccurrencename

egen ytdattendeddaycnt = total(monthdaysattendcntsum), by(schendyr stdpseudoid)
egen ytdenrolleddaycnt = total(monthdaysenrolledcntsum), by(schendyr stdpseudoid)

gen ytdabsentdaycnt = ytdenrolleddaycnt - ytdattendeddaycnt  

foreach var in gradecode schlocationcode schschoolname cdscode eduservctrcode {
	egen `var'_att = mode(`var'), by(schendyr stdpseudoid) minmode
}	

sort schendyr stdpseudoid occoccurrencename
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1] 

keep schendyr ytdenrolleddaycnt ytdattendeddaycnt ytdabsentdaycnt stdpseudoid ///
	gradecode_att schlocationcode_att schschoolname_att cdscode_att eduservctrcode_att

tostring gradecode_att, replace

compress
saveold "../data/lausd_data/raw_reshaped_dta/attendance_2004_2009_secondary.dta", replace

** Append all attendance together
use "../data/lausd_data/raw_reshaped_dta/attendance_2010_2014_all.dta", clear

append using "../data/lausd_data/raw_reshaped_dta/attendance_2004_2009_elem.dta"
append using "../data/lausd_data/raw_reshaped_dta/attendance_2004_2009_secondary.dta"

sort schendyr stdpseudoid 
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1]

compress
saveold "../data/lausd_data/raw_reshaped_dta/attendance_allstudentyrs.dta", replace

rm "../data/lausd_data/raw_reshaped_dta/attendance_2010_2014_all.dta" 
rm "../data/lausd_data/raw_reshaped_dta/attendance_2004_2009_elem.dta" 
rm "../data/lausd_data/raw_reshaped_dta/attendance_2004_2009_secondary.dta"


**** 8. PSAT Scores ****
use "../data/lausd_data/raw_appended_dta/psat_raw.dta", clear

sort schendyr stdpseudoid tsttestname  rawscore testpercentile
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1] & tsttestname == tsttestname[_n-1]

keep schendyr stdpseudoid rawscore testpercentile tsttestname gradecode schlocationcode schschoolname eduservctrcode

foreach var in gradecode schlocationcode schschoolname eduservctrcode {
	rename `var' `var'_psat 
}	

replace tsttestname = "math" if tsttestname == "MATHEMATICS"
replace tsttestname = "read" if tsttestname == "CRITICAL READING"
replace tsttestname = "overall" if tsttestname == "OVERALL"
replace tsttestname = "write" if tsttestname == "WRITING SKILLS"

rename rawscore psatscore 
rename testpercentile psatpct 

reshape wide psatscore psatpct, i(stdpseudoid schendyr) j(tsttestname) string

append using "../data/lausd_data/temp/q4_PSAT.dta"

compress
saveold "../data/lausd_data/raw_reshaped_dta/psat_raw_reshaped.dta", replace


**** 9. SAT Scores ****
use "../data/lausd_data/raw_appended_dta/sat_raw.dta", clear

rename year schendyr

duplicates tag schendyr stdpseudoid , gen(t)
assert t==0
drop t

sort schendyr stdpseudoid 
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1]

replace tooksat = "0" if tooksat == "No"
replace tooksat = "1" if tooksat == "Yes"

gen satmonth = substr(date,1,2)
replace satmonth = "" if satmonth == "NA"

gen satyear = substr(date,-2,4)
replace satyear = "" if satyear == "NA"
destring satyear satmonth, replace
replace satyear = satyear + 2000
replace satyear = 1999 if satyear == 2099

rename math satscoremath 
rename reading satscoreread 
rename writing satscorewrite
rename total satscoretotal 
rename mathile satpctmath
rename readile satpctread
rename writile satpctwrite

foreach var in satscoremath satscoreread satscorewrite satscoretotal satpctmath satpctread satpctwrite {
	replace `var' = "" if `var' == "NA"
}

drop date

foreach var in satpctmath satpctread satpctwrite {
	replace `var' = "0" if `var' == "-"
	replace `var' = "01" if `var' == "01-"
	replace `var' = "99" if `var' == "99+"
}

destring tooksat satscoremath satscoreread satscorewrite satscoretotal satpctmath satpctread satpctwrite, replace 
replace satyear = schendyr - 1 if satyear == 2099

compress
saveold "../data/lausd_data/raw_reshaped_dta/sat_raw_reshaped.dta", replace


**** 10. School Completion ****
use "../data/lausd_data/raw_appended_dta/school_completion_raw.dta", clear

rename year schendyr 
rename cdscode cdscode_grad
rename grade gradecode_grad
rename gender gender_grad

duplicates tag schendyr stdpseudoid , gen(t)
assert t==0
drop t

sort stdpseudoid schendyr
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1] 

replace graduated = "1" if graduated == "Graduated"
replace graduated = "" if graduated == "NA"
replace graduated = "0" if graduated == "Not Graduated"
destring graduated, replace

compress
saveold "../data/lausd_data/raw_reshaped_dta/graduation_raw_reshaped.dta", replace


**** 11. Suspensions ****
use "../data/lausd_data/raw_appended_dta/suspensions_raw.dta", clear

replace suspensiondayscnt = 0 if suspensiondayscnt < 0
egen dayssuspended = total(suspensiondayscnt), by(schendyr stdpseudoid)
egen numofsuspendions = count(suspensiondayscnt), by(schendyr stdpseudoid)

keep schendyr stdpseudoid dayssuspend numofsuspend gradecode schlocationcode schschoolname eduservctrcode

foreach var in gradecode schlocationcode schschoolname eduservctrcode {
	rename `var' `var'_suspend 
}

sort schendyr stdpseudoid
drop if schendyr == schendyr[_n-1] & stdpseudoid == stdpseudoid[_n-1]

compress
saveold "../data/lausd_data/raw_reshaped_dta/suspension_raw_reshaped.dta", replace


**** 12. Year to Date Attendance ****
** Done as part of 7. Attendance


**** 13. Secondary school attendance ****
** Done as part of 7. Attendance

**** 14. Work habit marks ****
forvalues year = 2006/2014 {
	
	use "../data/lausd_data/raw_appended_dta/workhabits_`year'_raw.dta", clear
	/*
	duplicates tag stdpseudoid schendyr workhabit semester, gen(t)
	assert t==0
	drop t
	*/
	sort stdpseudoid schendyr workhabit semester
	drop if stdpseudoid == stdpseudoid[_n-1] & workhabit == workhabit[_n-1] & ///
		schendyr == schendyr[_n-1] & semester == semester[_n-1]
	
	egen gradecode_workhabit = mode(grade), by(stdpseudoid) minmode
	keep semester stdpseudoid workhabit schendyr mark gradecode_workhabit	
	
	replace workhabit = "authority" if workhabit == "Accepts and respects authority"
	replace workhabit = "ontime" if workhabit == "Completes homework on time"
	replace workhabit = "cooperates" if workhabit == "Cooperates well in a group situation"
	replace workhabit = "socialinteraction" if workhabit == "Demonstrates appropriate social interaction with peers"
	replace workhabit = "fairplay" if workhabit == "Demonstrates fairplay"
	replace workhabit = "selfcontrol" if workhabit == "Exercises self-control"
	replace workhabit = "followsdirections" if workhabit == "Follows directions and procedures"
	replace workhabit = "useoftime" if workhabit == "Makes good use of time"
	replace workhabit = "organized" if workhabit == "Organizes materials"
	replace workhabit = "careful" if workhabit == "Presents neat and careful work"
	replace workhabit = "resolvesconflict" if workhabit == "Resolves conflict appropriately"
	replace workhabit = "dependable" if workhabit == "Shows dependability"
	replace workhabit = "responsible" if workhabit == "Takes responsibility"
	replace workhabit = "independent" if workhabit == "Works independently"

	rename mark habmark 

	reshape wide habmark, i(stdpseudoid schendyr semester) j(workhabit) string
	
	replace semester = lower(semester)

	reshape wide habmarkauthority-habmarkuseoftime, i(stdpseudoid schendyr) j(semester) string
	
	if `year'==2006{
		forvalues z = 1/3 {
			foreach var in habmarkauthorityep habmarkcarefulep habmarkcooperatesep habmarkdependableep habmarkfairplayep ///
			  habmarkfollowsdirectionsep habmarkindependentep habmarkontimeep habmarkorganizedep habmarkresolvesconflictep ///
			  habmarkresponsibleep habmarkselfcontrolep habmarksocialinteractionep habmarkuseoftimeep {
	
			  replace `var'`z' = "" if `var'`z' == "5" | `var'`z' == "~" | `var'`z' == "L" | `var'`z' == "R"
			}
		}

		destring habmarkauthorityep1-habmarkuseoftimeep3, replace
	}
	compress
	saveold "../data/lausd_data/raw_reshaped_dta/workhabits_`year'_raw_reshaped.dta", replace
}


*********************
*** C. Merge Data ***
*********************

forvalues year = 2003/2015 {

	** Start with the demographic data since it should have all student in it.  
	use "../data/lausd_data/raw_reshaped_dta/demographics_raw_reshaped.dta", clear
	keep if schendyr == `year'
	
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	
	**** 1. CAHSEE test scores **** 
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/cahsee_reshaped.dta", nogen 
	keep if schendyr == `year'

	**** 2. CELDT Test Scores ****
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/celdt_raw_reshaped.dta", nogen 
	keep if schendyr == `year'
	
	replace gradecode = gradecode_celdt if gradecode == ""
	replace schlocationcode = schlocationcode_celdt if schlocationcode == .
	replace schschoolname = schschoolname_celdt if schschoolname == ""
	replace cdscode = cdscode_celdt if cdscode == .
	replace eduservctrcode = eduservctrcode_celdt if eduservctrcode == ""
	
	drop gradecode_celdt schlocationcode_celdt schschoolname_celdt cdscode_celdt eduservctrcode_celdt
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	

	**** 3. CST Test Scores ****
	if `year'<=2014{
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/cst_`year'_raw_reshaped.dta", nogen
	keep if schendyr == `year'
	
	tostring gradecode_cst, replace 
	replace gradecode = gradecode_cst if gradecode == ""
	replace schlocationcode = schlocationcode_cst if schlocationcode == .
	replace schschoolname = schschoolname_cst if schschoolname == ""
	replace cdscode = cdscode_cst if cdscode == .
	replace eduservctrcode = eduservctrcode_cst if eduservctrcode == ""
	
	drop gradecode_cst schlocationcode_cst schschoolname_cst cdscode_cst eduservctrcode_cst
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	}

	**** 4. Elementary Marks ****
	if `year'<=2014{
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/elemmarks_`year'_raw_reshaped.dta", nogen
	keep if schendyr == `year'

	replace gradecode = gradecode_elemmarks if gradecode == ""
	replace schlocationcode = schlocationcode_elemmarks if schlocationcode == .
	replace schschoolname = schschoolname_elemmarks if schschoolname == ""
	replace eduservctrcode = eduservctrcode_elemmarks if eduservctrcode == ""
	
	drop gradecode_elemmarks schlocationcode_elemmarks schschoolname_elemmarks
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	}

	**** 5. Secondary Marks ****	
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/second_marks_`year'_raw_reshaped.dta", nogen
	keep if schendyr == `year'

	tostring gradecode_secondmarks, replace 
	replace gradecode = gradecode_secondmarks if gradecode == ""
	replace schlocationcode = schlocationcode_secondmarks if schlocationcode == .
	replace schschoolname = schschoolname_secondmarks if schschoolname == ""
	
	drop gradecode_secondmarks schlocationcode_secondmarks schschoolname_secondmarks
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	
	
	**** 6. Demographics ****
	** Started with this data 


	**** 7. Attendance ****
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/attendance_allstudentyrs.dta", nogen
	keep if schendyr == `year'

	replace gradecode = gradecode_att if gradecode == ""
	replace schlocationcode = schlocationcode_att if schlocationcode == .
	replace schschoolname = schschoolname_att if schschoolname == ""
	replace eduservctrcode = eduservctrcode_att if eduservctrcode == ""
	replace cdscode = cdscode_att if cdscode == .
	
	drop gradecode_att schlocationcode_att schschoolname_att eduservctrcode_att cdscode_att
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	

	**** 8. PSAT Scores ****
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/psat_raw_reshaped.dta", nogen
	keep if schendyr == `year'

	tostring gradecode_psat, replace
	replace gradecode = gradecode_psat if gradecode == ""
	replace schlocationcode = schlocationcode_psat if schlocationcode == .
	replace schschoolname = schschoolname_psat if schschoolname == ""
	replace eduservctrcode = eduservctrcode_psat if eduservctrcode == ""
	
	drop gradecode_psat schlocationcode_psat schschoolname_psat eduservctrcode_psat
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	

	**** 9. SAT Scores ****
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/sat_raw_reshaped.dta", nogen
	keep if schendyr == `year'
	

	**** 10. School Completion ****
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/graduation_raw_reshaped.dta", nogen
	keep if schendyr == `year'

	tostring gradecode_grad, replace	
	replace gradecode = gradecode_grad if gradecode == ""
	replace cdscode = cdscode_grad if cdscode == .
	rename gender_grad gender 
	
	drop gradecode_grad cdscode_grad 
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	

	**** 11. Suspensions ****
	merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/suspension_raw_reshaped.dta", nogen
	keep if schendyr == `year'
	
	replace gradecode = gradecode_suspend if gradecode == ""
	replace schlocationcode = schlocationcode_suspend if schlocationcode == .
	replace schschoolname = schschoolname_suspend if schschoolname == ""
	replace eduservctrcode = eduservctrcode_suspend if eduservctrcode == ""
	
	drop gradecode_suspend schlocationcode_suspend schschoolname_suspend eduservctrcode_suspend
	replace gradecode = "" if gradecode == "U" | gradecode == "."
	

	**** 12. Year to Date Attendance ****
	** Done as part of 7. Attendance

	**** 13. Secondary school attendance ****
	** Done as part of 7. Attendance

	**** 14. Work habit marks ****
	*** No Work habits data fro 2003 through 2005 until fixed data is sent by the LAUSD
	if `year'>=2006 & `year'<=2014{
		merge 1:1 schendyr stdpseudoid using "../data/lausd_data/raw_reshaped_dta/workhabits_`year'_raw_reshaped.dta", nogen
		keep if schendyr == `year'

		replace gradecode = gradecode_workhabit if gradecode == ""
		drop gradecode_workhabit
		replace gradecode = "" if gradecode == "U" | gradecode == "."
	}	
	compress
	saveold "../data/lausd_data/raw_merged_dta/raw_merged_`year'.dta", replace
}
*************************************
*** D. Append Long Data ***
*************************************
local cst_droplist "gradecode_cst schlocationcode_cst schschoolname_cst cdscode_cst eduservctrcode_cst"
local elemmarks_droplist "gradecode_elemmarks schlocationcode_elemmarks schschoolname_elemmarks"
local second_marks_droplist "gradecode schlocationcode schschoolname apsemesters"

foreach file in second_marks{
	forvalues year=2003/2014{
		if `year'==2003{
			use "../data/lausd_data/raw_long_dta/`file'_`year'_raw_long.dta", clear
		}
		else{
			append using "../data/lausd_data/raw_long_dta/`file'_`year'_raw_long.dta"
		}
	}
	ren schendyr year
	ren markforachievement achmark
	ren markforworkeffort  effmark
	ren markforcooperation coopmark
	
	drop ``file'_droplist'
	compress
	saveold "../data/lausd_data/raw_long_dta/`file'_all", replace
}


**************************
*** E. Clean Wide Data ***
*************************
forvalues YEAR = 2003/2015 {

	use "../data/lausd_data/raw_merged_dta/raw_merged_`YEAR'.dta", clear
	
	*** Clean variable by variable 
		**Cap is because we don't have cst scores in 2014
	drop occoccurrencename schschoolname eduservctrcode celdtoverall celdtlis_speak celdtreading celdtwriting  gender

	drop if gradecode == "PK" | gradecode == "TK" | gradecode == "0"
	replace gradecode = "0" if gradecode == "K"
	destring gradecode, replace
	rename gradecode grade 
			
	gen year = `YEAR'
	
	destring graduated, replace
	
	order stdpseudoid year 
	
	if `YEAR'<=2014{
	
		forvalues z = 1/3 {
			egen achmarktotalep`z' = rowmean(achmarkartep`z' achmarkeldlistenep`z' achmarkeldreadep`z' achmarkeldspeakep`z' achmarkeldwriteep`z' ///
				achmarkhealthep`z' achmarkhistep`z' achmarklistenep`z' achmarkmathep`z' achmarkpeep`z' achmarkreadep`z' achmarkscienceep`z' ///
				achmarkspeakep`z' achmarkwriteep`z')
		
			egen effmarktotalep`z' = rowmean(effmarkartep`z' effmarkeldlistenep`z' effmarkeldreadep`z' effmarkeldspeakep`z' effmarkeldwriteep`z' ///
				effmarkhealthep`z' effmarkhistep`z' effmarklistenep`z' effmarkmathep`z' effmarkpeep`z' effmarkreadep`z' effmarkscienceep`z' ///
				effmarkspeakep`z' effmarkwriteep`z')		
		}

		egen achmarktotalyr = rowmean(achmarkartep1 achmarkeldlistenep1 achmarkeldreadep1 achmarkeldspeakep1 achmarkeldwriteep1 ///
			achmarkhealthep1 achmarkhistep1 achmarklistenep1 achmarkmathep1 achmarkpeep1 achmarkreadep1 achmarkscienceep1 ///
			achmarkspeakep1 achmarkwriteep1 achmarkartep2 achmarkeldlistenep2 achmarkeldreadep2 achmarkeldspeakep2 achmarkeldwriteep2 ///
			achmarkhealthep2 achmarkhistep2 achmarklistenep2 achmarkmathep2 achmarkpeep2 achmarkreadep2 achmarkscienceep2 ///
			achmarkspeakep2 achmarkwriteep2 achmarkartep3 achmarkeldlistenep3 achmarkeldreadep3 achmarkeldspeakep3 achmarkeldwriteep3 ///
			achmarkhealthep3 achmarkhistep3 achmarklistenep3 achmarkmathep3 achmarkpeep3 achmarkreadep3 achmarkscienceep3 ///
			achmarkspeakep3 achmarkwriteep3)
		
		egen effmarktotalyr = rowmean(effmarkartep1 effmarkeldlistenep1 effmarkeldreadep1 effmarkeldspeakep1 effmarkeldwriteep1 ///
			effmarkhealthep1 effmarkhistep1 effmarklistenep1 effmarkmathep1 effmarkpeep1 effmarkreadep1 effmarkscienceep1 ///
			effmarkspeakep1 effmarkwriteep1 effmarkartep2 effmarkeldlistenep2 effmarkeldreadep2 effmarkeldspeakep2 effmarkeldwriteep2 ///
			effmarkhealthep2 effmarkhistep2 effmarklistenep2 effmarkmathep2 effmarkpeep2 effmarkreadep2 effmarkscienceep2 ///
			effmarkspeakep2 effmarkwriteep2 effmarkartep3 effmarkeldlistenep3 effmarkeldreadep3 effmarkeldspeakep3 effmarkeldwriteep3 ///
			effmarkhealthep3 effmarkhistep3 effmarklistenep3 effmarkmathep3 effmarkpeep3 effmarkreadep3 effmarkscienceep3 ///
			effmarkspeakep3 effmarkwriteep3)
				
		foreach var in achmarkart achmarkeldlisten achmarkeldread achmarkeldspeak achmarkeldwrite achmarkhealth ///
			achmarkhist achmarklisten achmarkmath achmarkpe achmarkread achmarkscience achmarkspeak achmarkwrite {	 
				egen `var'totalyr = rowmean(`var'ep1 `var'ep2 `var'ep3)
		}
		
		drop achmarkartep2-effmarkwriteep3	
		drop effmarkartep1 effmarkeldlistenep1 effmarkeldreadep1 effmarkeldspeakep1 effmarkeldwriteep1 effmarkhealthep1 effmarkhistep1 ///
			effmarklistenep1 effmarkmathep1 effmarkpeep1 effmarkreadep1 effmarkscienceep1 effmarkspeakep1 effmarkwriteep1
	}
		
		
	if `YEAR'>=2006 & `YEAR'<=2014{
		forvalues z = 1(1)3 {
			egen habmarktotalep`z' = rowmean(habmarkauthorityep`z' habmarkcarefulep`z' habmarkcooperatesep`z' habmarkdependableep`z' habmarkfairplayep`z' ///
				habmarkfollowsdirectionsep`z' habmarkindependentep`z' habmarkontimeep`z' habmarkorganizedep`z' habmarkresolvesconflictep`z' ///
				habmarkresponsibleep`z' habmarkselfcontrolep`z' habmarksocialinteractionep`z' habmarkuseoftimeep`z')
		}

		egen habmarktotalyr = rowmean(habmarkauthorityep1-habmarkuseoftimeep3)
		
		foreach var in habmarkauthority habmarkcareful habmarkcooperates habmarkdependable habmarkfairplay habmarkfollowsdirections habmarkindependent ///
			habmarkontime habmarkorganized habmarkresolvesconflict habmarkresponsible habmarkselfcontrol habmarksocialinteraction habmarkuseoftime {	 
				egen `var'totalyr = rowmean(`var'ep1 `var'ep2 `var'ep3)
		}
		
		forvalues z = 1(1)3 {
			egen habmarkfirst5totalep`z' = rowmean(habmarkcarefulep`z' habmarkindependentep`z' habmarkontimeep`z' habmarkorganizedep`z' habmarkuseoftimeep`z')
		}

		egen habmarkfirst5totalyr = rowmean(habmarkcarefulep1 habmarkindependentep1 habmarkontimeep1 habmarkorganizedep1 habmarkuseoftimeep1 ///
			habmarkcarefulep2 habmarkindependentep2 habmarkontimeep2 habmarkorganizedep2 habmarkuseoftimeep2 ///
			habmarkcarefulep3 habmarkindependentep3 habmarkontimeep3 habmarkorganizedep3 habmarkuseoftimeep3)

		forvalues z = 1(1)3 {
			egen habmarklast9totalep`z' = rowmean(habmarkauthorityep`z' habmarkcooperatesep`z' habmarkdependableep`z' habmarkfairplayep`z' ///
				habmarkfollowsdirectionsep`z' habmarkresolvesconflictep`z' habmarkresponsibleep`z' habmarkselfcontrolep`z' habmarksocialinteractionep`z') 
		}

		egen habmarklast9totalyr = rowmean(habmarkauthorityep1 habmarkcooperatesep1 habmarkdependableep1 habmarkfairplayep1 ///
			habmarkfollowsdirectionsep1 habmarkresolvesconflictep1 habmarkresponsibleep1 habmarkselfcontrolep1 habmarksocialinteractionep1 ///
			habmarkauthorityep2 habmarkcooperatesep2 habmarkdependableep2 habmarkfairplayep2 habmarkfollowsdirectionsep2 ///
			habmarkresolvesconflictep2 habmarkresponsibleep2 habmarkselfcontrolep2 habmarksocialinteractionep2 ///
			habmarkauthorityep3 habmarkcooperatesep3 habmarkdependableep3 habmarkfairplayep3 habmarkfollowsdirectionsep3 ///
			habmarkresolvesconflictep3 habmarkresponsibleep3 habmarkselfcontrolep3 habmarksocialinteractionep3) 

		drop habmarkauthorityep2-habmarkuseoftimeep3
		
		rename habmarkfollowsdirectionstotalyr habmarkfollowstotalyr
		rename habmarkresolvesconflicttotalyr habmarkresolvestotalyr
		rename habmarksocialinteractiontotalyr habmarksocialtotalyr
		
		foreach var of varlist grade-habmarklast9totalyr {
			*rename `var' `var'`YEAR'
		}
	}
	drop schendyr

	
	compress
	saveold "../data/lausd_data/clean_byyear_dta/clean_`YEAR'.dta", replace 
}


*****************************
*** Student Year Data Set ***
*****************************
adopath + .\Other_ado\
use ../data/lausd_data/clean_byyear_dta/clean_2003.dta, clear
forvalues year = 2004/2015{
	append using ../data/lausd_data/clean_byyear_dta/clean_`year'.dta
}

compress
gegen classid   = group(tchpseudoidfrommarks year) 
gegen teacherid = group(tchpseudoidfrommarks)
gegen studentid = group(stdpseudoid)

** Drop grade 13 students
drop if grade == 13 

*** the first studentid is was orginally 0 and is a bunch of different students who's student id number was missing.
drop if studentid == 1 

***** Clean outcome variables
*** Important outcome variables: graduation, took sat, sat score, attendance, suspensions, CST math scores, ///
	* CST english scores, CAHSEE scores, gpa, subject specific gpa, held back a grade, attrition from the LAUSD

** Create outcome variable that is ever graduated (1 if graduated by the end of the data, 
 * 0 if did not graduated by end of the data, . if data is not available 
 * or does not reach 12th grade by * last year of data)
 * Graduated variables largely only defined for students who reach 12th grade
gegen evergrad = total(graduated), by(studentid) missing 
replace evergrad = 1 if evergrad > 0 & evergrad < .
gegen maxgrade = max(grade), by(studentid)
gegen maxyear = max(year), by(studentid) 


** Create outcome variables that is ever graduated (1 if graduated by the end of the data, 
	* 0 if did not graduated by end of the data and have reached 
	* grade 9, 10 ,11, or 12, . if data is not available or does not reach 12th grade by last year of data)
gegen enter9thgradeby2012 = max((grade == 9) & (year <= 2012)), by(studentid)
gegen enter10thgradeby2013 = max((grade == 10) & (year <= 2013)), by(studentid)
gegen enter11thgradeby2014 = max((grade == 11) & (year <= 2014)), by(studentid)
gegen enter12thgradeby2015 = max((grade == 12) & (year <= 2015)), by(studentid)
	
gen evergrad9=evergrad 
replace evergrad9 = 0 if (enter9thgradeby2012==1) & (evergrad==.)
replace evergrad9 = . if (enter9thgradeby2012==0)

gen evergrad10=evergrad 
replace evergrad10 = 0 if (enter10thgradeby2013==1) & (evergrad==.)
replace evergrad10 = . if (enter10thgradeby2013==0)

gen evergrad11=evergrad 
replace evergrad11 = 0 if (enter11thgradeby2014==1) & (evergrad==.)
replace evergrad11 = . if (enter11thgradeby2014==0)

gen evergrad12=evergrad 
replace evergrad12 = 0 if (enter12thgradeby2015==1) & (evergrad==.)
replace evergrad12 = . if (enter12thgradeby2015==0)

replace evergrad = . if maxgrade < 12 & evergrad == 0 

drop enter10thgradeby2013 enter11thgradeby2014 enter12thgradeby2015

** Create outcome variable that is took the SAT
gegen eversat = total(tooksat), by(studentid) missing 
replace eversat = 1 if eversat > 0 & eversat < . 
replace eversat = 0 if eversat != 1 
replace eversat = . if maxgrade < 12

** Change SAT total score to be from 600 to 2400. I only the math and reading section are used (no writing section) then add a weight of 1.5 
replace satscoretotal = 1.5*satscoretotal if satscorewrite == . 

** Create outcome variable that is took the PSAT 
gen tookpsat = (psatscoreoverall != .) 

gegen everpsat = total(tookpsat), by(studentid) missing 
replace everpsat = 1 if everpsat > 1 & everpsat < .
replace everpsat = . if maxgrade < 10 | maxyear < 2009

** Change PSAT total score to be on a from 600 to 2400 scale and the subscores to be on a 200 to 800 scale so that they match the SAT scoring scale. 
replace psatscoremath = 10*psatscoremath 
replace psatscoreread = 10*psatscoreread 
replace psatscorewrite = 10*psatscorewrite 
replace psatscoreoverall = 10*psatscoreoverall 


*** Make SAT scores and PSAT scores constant across all years for  a student (therefore don't neet the lead operator) 
foreach var in satscoremath satscoreread satscorewrite satscoretotal satpctmath satpctread satpctwrite ///
	psatscoremath psatpctmath psatscoreoverall psatpctoverall psatscoreread psatpctread psatscorewrite psatpctwrite{
	gegen tempvar = mean(`var'), by(studentid)
	replace `var' = tempvar
	drop tempvar
}

*** Clean attendance data
foreach var in ytdenrolleddaycnt ytdattendeddaycnt ytdabsentdaycnt{ 
	replace `var' = 180 if `var' > 180 & `var' < .
}

rename ytdenrolleddaycnt daysenrolled
rename ytdattendeddaycnt daysattended 
rename ytdabsentdaycnt daysabsent

gen fracdaysabsent = daysabsent/daysenrolled
gen logdaysabsent = log(daysabsent + 1) 

** Create high school total attendace data
gegen hs_daysabsent = total(daysabsent) if grade >= 9 & grade <= 12, by(studentid) missing 
replace hs_daysabsent = 720 if hs_daysabsent > 720 & hs_daysabsent < .
gen hs_logdaysabsent = log(hs_daysabsent)

gegen hs_daysenrolled = total(daysenrolled) if grade >= 9 & grade <= 12, by(studentid) missing
replace hs_daysenrolled = 720 if hs_daysenrolled > 720 & hs_daysenrolled < .

gen hs_fracdaysabsent = hs_daysabsent/hs_daysenrolled

foreach var in hs_daysabsent hs_daysenrolled hs_fracdaysabsent hs_logdaysabsent {
	sort studentid `var'
	by studentid: replace `var' = `var'[_n-1] if `var' == . 
}

** Make days suspended be count data and have equal to zero if never suspended 
replace dayssuspended = ceil(dayssuspended)
replace dayssuspended = 0 if dayssuspended == .
gen suspended = (dayssuspended > 0)

*** Create high school total days suspended
gegen hs_dayssuspended = total(dayssuspended) if grade >= 9 & grade <= 12, by(studentid) missing 

foreach var in hs_dayssuspended {
	sort studentid `var'
	by studentid: replace `var' = `var'[_n-1] if `var' == . 
}

gen hs_suspended = (hs_dayssuspended > 0)

** Make number of suspensions equal to zero if never suspended 
replace numofsuspendions = 0 if numofsuspendions == .

** Create standardized CAHSEE scores by year
gegen cahseeelamean = mean(cahseescoreela), by(year)
gegen cahseemathmean = mean(cahseescoremath), by(year)
egen cahseeelasd = sd(cahseescoreela), by(year)
egen cahseemathsd = sd(cahseescoremath), by(year)

gen zcahseescoreela = (cahseescoreela - cahseeelamean)/cahseeelasd if cahseescoreela != . 
gen zcahseescoremath = (cahseescoremath - cahseemathmean)/cahseemathsd if cahseescoremath != . 
drop cahseeelamean cahseeelasd cahseemathmean cahseemathsd 

foreach var in cahseescoreela cahseepassedela cahseeattemptsela cahseescoremath cahseepassedmath cahseeattemptsmath zcahseescoreela zcahseescoremath {
	sort studentid `var'
	by studentid: replace `var' = `var'[_n-1] if `var' == . 
}


*** Create GPA for all grades (combine elemetary and secondary GPA measures) 
replace gpatotal = achmarktotalyr if (gpatotal == .) | (grade <= 5) 

* Combine ELD and Non-ELD grades
foreach var in listen read speak write{
	egen ck = rowmean(achmarkeld`var'totalyr achmark`var'totalyr)
	drop achmarkeld`var'totalyr achmark`var'totalyr
	ren ck gpa`var'
}
ren achmarkhisttotalyr achmarksocialtotalyr

* Combine elementry English grades
egen achmarkenglishtotalyr = rowmean(gpalisten gpaspeak gpawrite gparead)

foreach var in english math science social health pe art{
	replace gpa`var' = achmark`var'totalyr if (gpa`var' == .) | (grade <= 5) 
}


*** Create high school total GPA
foreach postfix in gpa effort coop{
	replace classcount_`postfix' = 20 if classcount_`postfix' > 20 & classcount_`postfix' < .
}

gen gpatotalwghtyr = gpatotal*classcount_gpa
gen workeffortgpatotalwghtyr = workeffortgpatotal*classcount_effort
gen coopgpatotalwghtyr = coopgpatotal*classcount_coop

gegen gpatotaltemp = total(gpatotalwghtyr) if grade >= 9 & grade <= 12, by(studentid) missing 
gegen workefforttemp = total(workeffortgpatotalwghtyr) if grade >= 9 & grade <= 12, by(studentid) missing 
gegen cooptemp = total(coopgpatotalwghtyr) if grade >= 9 & grade <= 12, by(studentid) missing 
gegen classcounttemp_gpa = total(classcount_gpa) if grade >= 9 & grade <= 12, by(studentid) missing 
gegen classcounttemp_effort = total(classcount_effort) if grade >= 9 & grade <= 12, by(studentid) missing 
gegen classcounttemp_coop = total(classcount_coop) if grade >= 9 & grade <= 12, by(studentid) missing 

gen hs_gpatotal = gpatotaltemp/classcounttemp_gpa
gen hs_workeffortgpatotal = workefforttemp/classcounttemp_effort
gen hs_coopgpatotal = cooptemp/classcounttemp_coop

drop gpatotaltemp workefforttemp cooptemp classcounttemp* gpatotalwghtyr workeffortgpatotalwghtyr coopgpatotalwghtyr

foreach var in hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal{
	sort studentid `var'
	by studentid: replace `var' = `var'[_n-1] if `var' == . 
}

*** Create held back a year 
xtset studentid year

gen heldback = . 
replace heldback = 0 if ((grade+1) == F.grade) & (grade != .) & (F.grade != .)
replace heldback = 1 if (grade == F.grade) & (grade != .) & (F.grade != .) 
replace heldback = 0 if graduated==1

gegen hs_heldback = total(heldback) if grade >= 9 & grade <= 11, by(studentid) missing 
replace hs_heldback = 1 if hs_heldback > 0 & hs_heldback < .

foreach var in hs_heldback {
	sort studentid `var'
	by studentid: replace `var' = `var'[_n-1] if `var' == . 
}

*** Leave the LAUSD
xtset studentid year

sum year

gen leavelausd = .
replace leavelausd = 1 if F.year == . & year != r(max) 
replace leavelausd = 0 if F.year != . & year != r(max) 
replace leavelausd = . if grade == 12

*** Create high school total of leave LAUSD
gegen hs_leavelausd = total(leavelausd) if grade >= 9 & grade <= 11, by(studentid) missing 
replace hs_leavelausd = 1 if hs_leavelausd > 0 & hs_leavelausd < .

foreach var in hs_leavelausd {
	sort studentid `var'
	by studentid: replace `var' = `var'[_n-1] if `var' == . 
}

*** Create variable for entering high school but not graduating (either because the student leaves the school district or doesn't graduate)
gen hs_dropout = . 
replace hs_dropout = 0 if enter9thgradeby2012 == 1 & evergrad == 1
replace hs_dropout = 1 if enter9thgradeby2012 == 1 & (evergrad == 0 | evergrad == .) 

drop enter9thgradeby2012 

*** Create CST science and social science tests scores (They are constant across all years for a student and therefore don't need the lead operator) 
foreach var in zcstsocial8 zcstsocial11 zcstsocialworld zcstscience5 zcstscience8 zcstscience10{
	gegen tempvar = mean(`var'), by(studentid)
	replace `var' = tempvar
	drop tempvar
}

*** Create total number of AP classes a student took (a AP class is defined as an AP class for two semesters) 
gegen apclasstotal = total(apsemesters), by(studentid)
replace apclasstotal = apclasstotal/2 
replace apclasstotal = . if maxgrade < 9 

*** Create if an ELL 
gen ell = (zceldtoverall != .)

*** Create High School Tracking Measures
foreach var of varlist apmath_classes/* trackmath_a trackmath_b trackmath_c trackmath_r /// 
	apenglish_classes ela_elective ela_helper*/{
	replace `var'=. if grade<6
	gegen hs_`var' = total(`var'), by(studentid)
}


** Drop non-standarized variables
cap drop cstscoreela cstscoremath cstscorescience5 cstscorescience8 cstscorescience10 cstscoresocial8 ///
	cstscoresocial11 cstscoresocialworld cahseescoreela cahseescoremath

** Create High School Outcome Indices
* Standardize outcomes
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback {
		egen fn_`var' = std(`var')
}

* Make indicies
gen hs_zalltest   = fn_zcahseescoremath + fn_zcahseescoreela + fn_satscoretotal
gen hs_zexittest = fn_zcahseescoremath + fn_zcahseescoreela 
gen hs_znontest   = -1*fn_hs_dropout + fn_eversat + fn_hs_gpatotal + fn_hs_workeffortgpatotal + fn_hs_coopgpatotal - ///
					      fn_hs_dayssuspended - fn_hs_logdaysabsent - fn_hs_heldback 
gen hs_zcahsee       = fn_zcahseescoremath + fn_zcahseescoreela
gen hs_zbehave    = fn_hs_gpatotal - fn_hs_dayssuspended - fn_hs_logdaysabsent - hs_heldback 
gen hs_zbehavlong = fn_hs_gpatotal + fn_hs_workeffortgpatotal + fn_hs_coopgpatotal - ///
					   fn_hs_dayssuspended - fn_hs_logdaysabsent - fn_hs_heldback 
gen hs_zgpa 	  = fn_hs_gpatotal + fn_hs_workeffortgpatotal + fn_hs_coopgpatotal 


foreach var in gpatotal workeffortgpatotal coopgpatotal{
	ren fn_hs_`var' hs_z`var'
}

* Drop unneeded vars
foreach var in hs_dropout eversat satscoretotal   ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback {
		drop fn_`var' 
}

* Standardize indices
foreach var of varlist hs_zalltest hs_znontest hs_zcahsee hs_zbehave hs_zbehavlong hs_zgpa{
	egen z=std(`var')
	replace `var' = z
	drop z
}
	
	
** Normalized GPA 
foreach var in gpatotal effmarktotalyr habmarktotalyr habmarkfirst5totalyr habmarklast9totalyr  /*gpaenglish gpamath gpascience gpasocial gpalisten ///
	gparead gpaspeak gpawrite gpahealth gpalang gpape gpaart*/{
	gegen `var'mean = mean(`var'), by(year grade) 
	egen `var'sd = sd(`var'), by(year grade) 
	gen z`var' = (`var' - `var'mean)/`var'sd
	drop `var'mean `var'sd
}	

gegen class_size = count(studentid), by(classid year)
compress
** Attrition
xtset studentid year
gen attrition=studentid!=. & F.studentid==. if grade<12

** Previous Year Individual and Class Averages (if used in later analysis)
xtset studentid year
foreach var in habmarkfirst5totalyr habmarklast9totalyr zcstmath zcstela gpatotal effmarktotalyr ///
	logdaysabsent suspended heldback ell habmarkfirst5totalep3 habmarklast9totalep3  ///
	gpaenglish gpamath gpascience gpasocial gpalisten gparead gpaspeak gpawrite gpahealth gpalang gpape gpaart gpaother{
		gen L`var'=L.`var'
		gen L2`var'=L.`var'
		if (inlist("`var'","gpaenglish","gpamath","gpascience","gpasocial","gpalisten","gparead")==0) & /// 
			(inlist("`var'","gpaspeak","gpawrite","gpahealth","gpalang","gpape","gpaart","gpaother")==0){
			gegen grade_L`var' = mean(L`var'), by(cdscode grade year)
			gegen class_L`var' = mean(L`var'), by(classid year)
		}	
			
		if inlist("`var'","habmarkfirst5totalyr","habmarklast9totalyr","habmarkfirst5totalep3","habmarklast9totalep3")==1 {
			drop L`var' L2`var'
		}
}

** Cubics
foreach var in habmarkfirst5totalyr habmarklast9totalyr zcstmath zcstela gpatotal effmarktotalyr ///
	logdaysabsent suspended heldback ell habmarkfirst5totalep3 habmarklast9totalep3   ///
	gpaenglish gpamath gpascience gpasocial gpalisten gparead gpaspeak gpawrite gpahealth gpalang gpape gpaart gpaother{
    forvalues x=2/3{
		gen `var'`x'=`var'^`x'
		if (inlist("`var'","gpaenglish","gpamath","gpascience","gpasocial","gpalisten","gparead")==0) & ///
			(inlist("`var'","gpaspeak","gpawrite","gpahealth","gpalang","gpape","gpaart","gpaother")==0){
			gen grade_L`var'`x'=grade_L`var'^`x'
			gen class_L`var'`x'=class_L`var'^`x'
		}
		if inlist("`var'","habmarkfirst5totalyr","habmarklast9totalyr","habmarkfirst5totalep3","habmarklast9totalep3")==0{
			gen L`var'`x'=L`var'^`x'
			gen L2`var'`x'=L2`var'^`x'
		}
	}
}

forvalues x=2/3{
	gen class_size`x'=class_size^`x'
}

** Leads
foreach var in gpatotal effmarktotalyr  logdaysabsent suspended heldback ///
	gpalisten gparead gpawrite gpaspeak ///
	gpamath gpaenglish gpaart gpahealth gpalang gpape gpascience gpasocial gpaother{	
	gen F`var'=F.`var'
}

*** Number of schools and grades that each teacher teaches at
sort year teacherid cdscode
egen seq=seq(), by(year teacherid cdscode)
gegen num_school_teacher_year = total(seq==1) if  year!=. & teacherid!=. & cdscode!=., by(teacherid year)

sort year teacherid grade
egen seq2=seq(), by(year teacherid cdscode)
gegen num_grade_teacher_year = total(seq2==1) if  year!=. & teacherid!=. & grade!=., by(teacherid year)


*** Number of students each teacher has per year, per school-year
gegen students_peryear = count(studentid) if teacherid!=. & year!=., by(teacherid year)
gegen students_perschoolyear = count(studentid) if  year!=. & teacherid!=. & cdscode!=., by(teacherid cdscode year)
gegen maxstudents_perschoolyear = max(students_perschoolyear), by(teacherid year)

*** Implement Sample Restrictions (based on Chetty at pp. 2640-2641)
gen exclude_flag=0
replace exclude_flag = 1  if (students_peryear >50        | students_peryear <10)       & students_peryear!=.
replace exclude_flag = 1  if (students_perschoolyear >50  | students_perschoolyear <10) & students_peryear!=.

drop if exclude_flag==1
replace teacherid=. if maxstudents_perschoolyear!=students_perschoolyear

* Note: Don't have special education status and special education only schools appear to not be
* in the data.

compress
*** Grade 11 or 12 Variables
foreach var in gpatotal workeffortgpatotal coopgpatotal dayssuspended logdaysabsent {
	gen tempvar12=`var' if grade==12
	gegen `var'12 = mean(tempvar12), by(studentid)
	drop tempvar12
}
foreach var in  zcstmath zcstela heldback{
	gen tempvar11=`var' if grade==11
	gegen `var'11 = mean(tempvar11), by(studentid)
	drop tempvar11
}


gegen long teacher_grade=group(teacherid grade)

*** Make data set smaller by dropping unneeded information
drop if grade < 2
drop achmarkartep1-eduservctrcode_elemmarks workeffortgpaart coopgpaart /// 
	 workeffortgpaenglish coopgpaenglish workeffortgpahealth coopgpahealth workeffortgpalang coopgpalang /// 
	 workeffortgpamath coopgpamath workeffortgpaother coopgpaother workeffortgpape coopgpape workeffortgpascience /// 
	 coopgpascience workeffortgpasocial coopgpasocial achmarktotalep1-effmarktotalep3 schlocationcode /// 
	 cahseepassedela-cahseeattemptsmath celdttype-zceldtwriting classcount_gpa-classcount_coop /// 
	 daysenrolled-daysabsent psatscoremath psatpctmath 	psatpctoverall-psatpctwrite tooksat satscoremath-satscorewrite /// 
	 satpctmath-satyear  achmarkarttotalyr-achmarksciencetotalyr habmarkauthorityep1-habmarkuseoftimeep1 habmarktotalep2 ///
	 habmarkfirst5totalep2 habmarklast9totalep2 maxgrade maxyear tookpsat hs_daysabsent hs_daysenrolled seq seq2 ///
	 num_school_teacher_year students_peryear students_perschoolyear num_grade_teacher_year maxstudents_perschoolyear exclude_flag ///
	 eduservctrcode_secondmarks graduated

* Compute Teacher VA seperately by grade
gegen modal_grade=mode(grade), by(teacherid year)
gegen long teacherid_grade =group(teacherid modal_grade)

order year studentid teacherid grade		 
compress 
saveold ../data/lausd_data/clean_append/student_year_all, replace


*****************************
*** Student-Class Data Set ***
*****************************
#delimit; 
local keeplist "grade cdscode studentid 
                zcstmath  zcstela  gpatotal  effmarktotalyr  logdaysabsent  suspended heldback effmarktotalyr
				Lgpaart Lgpaenglish Lgpahealth Lgpalang Lgpamath Lgpape Lgpascience Lgpasocial Lgpaother
                zcstmath? zcstela? gpatotal? effmarktotalyr?
				Lgpaart? Lgpaenglish? Lgpahealth? Lgpalang? Lgpamath? Lgpape? Lgpascience? Lgpasocial? Lgpaother?
				Lzcstmath Lzcstela Lgpatotal Leffmarktotalyr Llogdaysabsent Lsuspended Lheldback Leffmarktotalyr
                Lzcstmath? Lzcstela? Lgpatotal? Leffmarktotalyr?
				L2zcstmath L2zcstela L2gpatotal L2effmarktotalyr L2logdaysabsent L2suspended L2heldback L2effmarktotalyr
                L2zcstmath? L2zcstela? L2gpatotal? L2effmarktotalyr?
				grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
				grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? 
				grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell ell
				Fgpatotal Feffmarktotalyr Flogdaysabsent Fsuspended Fheldback Fgpaart Fgpaenglish 
				Fgpahealth Fgpalang Fgpamath Fgpape Fgpascience Fgpasocial Fgpaother";
#delimit cr;

use ../data/lausd_data/raw_long_dta/second_marks_all.dta, clear
drop eduservctrcode
* drop other to save space
* drop if crstype=="other"

* Analyze at the year level
collapse (mean) achmark effmark coopmark (max) classid (count) num_grades=achmark ///
		 (sum) apmath_classes trackmath_a trackmath_b trackmath_c trackmath_r /// 
			   apenglish_classes ela_elective ela_helper ///
			, by(year stdpseudoid tchpseudoid crstype)

*** Add Student Level Variables Needed for VA Calculation
merge m:1 stdpseudoid year using ../data/lausd_data/clean_append/student_year_all, nogen ///
	keepusing(`keeplist') keep(match)

gegen teacherid = group(tchpseudoid)
order studentid teacherid year	
	
*** Drop likely mis-matches (based on Chetty at pp. 2640-2641)
* Number of students each teacher has per semester, per school-year
gegen students_peryear = total(num_grades) if teacherid!=. & year!=., by(teacherid year)
replace students_peryear = students_peryear / 2
gegen students_perschoolyear = total(num_grades) if  year!=. & teacherid!=. & cdscode!=., by(teacherid cdscode year)
replace students_perschoolyear = students_perschoolyear / 2
gegen maxstudents_perschoolyear = max(students_perschoolyear), by(teacherid year)

* Implement Sample Restrictions 
gen exclude_flag=0
replace exclude_flag = 1  if (students_peryear >400       | students_peryear <20)       & students_peryear!=.
replace exclude_flag = 1  if (students_perschoolyear >400 | students_perschoolyear <20) & students_peryear!=.

drop if exclude_flag==1
replace teacherid=. if round(maxstudents_perschoolyear,1)!=round(students_perschoolyear,1)

drop students_peryear students_perschoolyear maxstudents_perschoolyear exclude_flag
	
** Previous Year Class Averages
foreach var in zcstmath zcstela gpatotal effmarktotalyr logdaysabsent suspended heldback {
		gegen class_L`var' = mean(L`var'), by(classid year)
}
gegen class_ell = mean(ell), by(classid year)
** Cubics
foreach var in zcstmath zcstela gpatotal effmarktotalyr logdaysabsent suspended heldback{
	forvalues x=2/3{
		gen class_L`var'`x'=class_L`var'^`x'
	}
}
gen grade_Leffmarktotalyr2=grade_Leffmarktotalyr^2
gen grade_Leffmarktotalyr3=grade_Leffmarktotalyr^3
gegen class_size = count(studentid), by(classid)


compress
saveold ../data/lausd_data/clean_append/student_class_year_all, replace	

******************************************
*** Create Modal HS CDS for clustering ***
******************************************
use  year studentid grade cdscode using ../data/lausd_data/clean_append/student_year_all, clear 

* Arbitrarily pick school when student has more than one in same grade-year
collapse (min) cdscode, by(studentid grade year)
destring grade, replace
drop if grade==. | cdscode==.
* Use first observation of each grade
sort studentid grade year
egen seq=seq(), by(studentid grade)
drop if seq>1
egen year_gr5 = max(year*(grade==5)), by(studentid) 
drop year seq


* Identify the modal school HS students transition to out of elementary school and middle school
* in each year
reshape wide cdscode, i(studentid) j(grade)
foreach elgrade in 5 8{
	egen el`elgrade'_hs9=mode(cdscode9) if cdscode`elgrade'!=., by(cdscode`elgrade') minmode
}
drop if year_gr==0
ren year_gr year
* What share of students end up in Modal HS?
**(about half)
egen fifth_to_hs=mean(el5_hs9==cdscode9) if el5_hs9!=. & cdscode9!=. & cdscode5!=.
egen eighth_to_hs=mean(el8_hs9==cdscode9) if el8_hs9!=. & cdscode9!=. & cdscode9!=.
sum fifth eighth
drop fifth eighth
saveold ../data/lausd_data/temp/grade_cdspairs, replace

keep studentid cdscode5 cdscode8
* Can't easily reshape long in the way we want, so reshape and merge
reshape long cdscode, i(studentid) j(grade)
keep if cdscode !=.
merge m:1 studentid using ../data/lausd_data/temp/grade_cdspairs , keepusing(el5_hs9 el8_hs9) keep(1 3) nogen
* Create variable with HS people typically end up in
gen hs_cdscode=.
foreach grade in 5 8{
	replace hs_cdscode = el`grade'_hs9 if grade==`grade'
}
drop if hs_cdscode==.
* Small number of cases where there is more than on modal match
* Across 5th and 8th grades. Likely due to change in classification of schools
* or errors in data entry. Keep most common match	
collapse (count) grade, by(cdscode hs_cdscode)
egen mcount=max(grade), by(cdscode)
drop if grade!=mcount
drop mcount grade
saveold ../data/lausd_data/temp/cdscode_hscdscode, replace

	
***********************************************************
*** Calculate VA Scores - Middle School and High School ***
***********************************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "gpatotal logdaysabsent suspended heldback";

local jackson_va_vars	"gpatotal logdaysabsent suspended heldback ";	

* effmarktotalyr effmarktotalyr Leffmarktotalyr Leffmarktotalyr? grade_Leffmarktotalyr class_Leffmarktotalyr class_Leffmarktotalyr? grade_Leffmarktotalyr?;
	
local controls "i.grade#C.(Lzcstmath Lzcstela Lgpatotal  Lzcstmath? 
	Lzcstela? Lgpatotal?  Llogdaysabsent Lsuspended Lheldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal  
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?  
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_ell class_size) 
	ell i.grade i.year";

* Only include core subject GPA's because many people do not take noncore classes
* Will add subject-specific controls in VAM calc to cover these cases
local controls_subjectgpa "i.grade#C.(Lzcstmath Lzcstela Lgpatotal  Lzcstmath? 
	Lzcstela? Lgpatotal?  Llogdaysabsent Lsuspended Lheldback
	Lgpaenglish Lgpamath Lgpascience Lgpasocial
	Lgpaenglish? Lgpamath? Lgpascience? Lgpasocial?)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal  
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?  
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_ell class_size) 
	ell i.grade i.year";
	
#delimit cr;

**Note: Need to include an absorb because the Stata version of VAM is set at 10.1, 
** and in that version reg cannot accept factor variables.

*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end
cap matrix drop varmat

foreach subject in math english art /*health*/ lang pe science social{
	use ../data/lausd_data/clean_append/student_class_year_all if crstype=="`subject'" & grade>5, clear
	* only use students with one teacher per subject
	duplicates tag studentid year, gen(t)
	drop if t>0
	drop t
	
	xtset studentid year

	* Test Scores
	foreach testscore in `standard_va_vars'{
		vam `testscore', teacher(teacherid) year(year) class(classid) ///
			controls(`controls') absorb(year) quasiex /// 
			driftlimit(6) output(../data/lausd_data/vascores/secondary/`testscore'_`subject')
	}


	* Other Outcomes
	foreach grade in  `leaveout_va_vars'{
		vam F`grade', teacher(teacherid) year(year) class(classid) ///
			controls(`controls') absorb(year) quasiex /// 
			driftlimit(5) output(../data/lausd_data/vascores/secondary/`grade'_`subject')
	}

	* Jackson Replication
	foreach var in `jackson_va_vars'{
		vam `var', teacher(teacherid) year(year) class(classid) ///
			controls(`controls') absorb(year) quasiex /// 
			driftlimit(6) output(../data/lausd_data/vascores/secondary/jack_`var'_`subject')
	}
	* Subject GPA
	vam Fgpa`subject', teacher(teacherid) year(year) class(classid) ///
		controls(Lgpa`subject' Lgpa`subject'? `controls_subjectgpa') absorb(year) /// 
		driftlimit(5) output(../data/lausd_data/vascores/secondary/gpa`subject')

	foreach va in `standard_va_vars' `leaveout_va_vars'{
		insheet using "../data/lausd_data/vascores/secondary/`va'_`subject'_variance.csv", comma clear
		makevarmat
	}
		
	foreach va in `jackson_va_vars' {
		insheet using "../data/lausd_data/vascores/secondary/jack_`va'_`subject'_variance.csv", comma clear
		makevarmat
	}
	insheet using "../data/lausd_data/vascores/secondary/gpa`subject'_variance.csv", comma clear
	makevarmat

	
	* Combine VA data sets
	local counter=1
	foreach va in `standard_va_vars'{
		if `counter'==1{
			use ../data/lausd_data/vascores/secondary/`va'_`subject', clear
			ren tv tva_`va'
		}
		if `counter'>1{
			mmerge teacherid year using ../data/lausd_data/vascores/secondary/`va'_`subject', type(n:1) unmatched(both) ///
				ukeep(tv) urename(tv tva_`va'_`subject')
		}
		local ++counter
	}				

	foreach va in `leaveout_va_vars'{
		mmerge teacherid year using ../data/lausd_data/vascores/secondary/`va'_`subject', type(n:1) unmatched(both) ///
			ukeep(tv) urename(tv tva_`va'_`subject')

	}

	foreach va in `jackson_va_vars' {
		mmerge teacherid year using ../data/lausd_data/vascores/secondary/jack_`va'_`subject', type(n:1) unmatched(both) ///
			ukeep(tv) urename(tv j_tva_`va'_`subject')
	}
	mmerge teacherid year using ../data/lausd_data/vascores/secondary/gpa`subject', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv tva_gpa`subject')


	drop _merge

	local counter=1

	foreach va in `standard_va_vars'{
		gen ztva_`va' = tva_`va'/(varmat[1,`counter']^0.5) 
		local ++counter
	}

	foreach va in `leaveout_va_vars'{
		gen ztva_`va' = tva_`va'/(varmat[1,`counter']^0.5) 
		local ++counter
	}

	foreach va in `jackson_va_vars'{
		gen j_ztva_`va' = j_tva_`va'/(varmat[1,`counter']^0.5) 
		local ++counter
	}
	gen ztva_gpa`subject' = tva_gpa`subject'/(varmat[1,`counter']^0.5) 
	local ++counter

	drop tva_* j_tva_* 

	saveold ../data/lausd_data/vascores/secondary/va_secondary_`subject'_all, replace
}
	
************************************************
*** Calculate VA Scores - Elementary School  ***
************************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "habmarkfirst5totalyr habmarklast9totalyr habmarktotalyr 
	habmarkauthoritytotalyr habmarkcarefultotalyr habmarkcooperatestotalyr habmarkdependabletotalyr 
	habmarkfairplaytotalyr habmarkfollowstotalyr habmarkindependenttotalyr habmarkontimetotalyr 
	habmarkorganizedtotalyr habmarkresolvestotalyr habmarkresponsibletotalyr habmarkselfcontroltotalyr 
	habmarksocialtotalyr habmarkuseoftimetotalyr gpatotal effmarktotalyr  logdaysabsent suspended heldback";

local leaveout_va_vars_ep1 "habmarkfirst5totalep1 habmarklast9totalep1 habmarktotalep1";	

local jackson_va_vars	"gpatotal logdaysabsent suspended heldback effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr";	

local other_robustness_vars "zcstmath zcstela";

local grade_vars "gpaenglish gpamath gpascience gpasocial gpalisten gparead gpaspeak gpawrite gpahealth gpape gpaart";
	
local controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
	
local controls_ep3 "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalep3 L.habmarklast9totalep3 L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalep3? L.habmarklast9totalep3? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalep3 grade_Lhabmarklast9totalep3 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalep3? 
	grade_Lhabmarklast9totalep3? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalep3 class_Lhabmarklast9totalep3
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalep3? 
	class_Lhabmarklast9totalep3? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_grades "i.grade#C.(L.zcstmath L.zcstela L.gpamath L.gpascience L.gpasocial L.gpalisten L.gparead L.gpaspeak 
	L.gpawrite L.gpahealth L.gpape L.gpaart L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.gpamath? L.gpascience?   
	L.gpasocial? L.gpalisten? L.gparead? L.gpaspeak? L.gpawrite? L.gpahealth?  L.gpape? L.gpaart?	L.logdaysabsent 
	L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";	
	
local controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size
	habmarkfirst5totalep3 habmarklast9totalep3 habmarkfirst5totalep3? habmarklast9totalep3? 
	grade_Lhabmarkfirst5totalep3 grade_Lhabmarklast9totalep3 grade_Lhabmarkfirst5totalep3? 
	grade_Lhabmarklast9totalep3? class_Lhabmarkfirst5totalep3 class_Lhabmarklast9totalep3 class_Lhabmarkfirst5totalep3? 
	class_Lhabmarklast9totalep3? gpaenglish? gpamath? gpascience? gpasocial? gpalisten? gparead? gpaspeak? gpawrite? 
	gpahealth? gpape? gpaart? ell";
	
#delimit cr

use studentid year grade teacherid classid `standard_va_vars'  ///
	`leaveout_va_vars' `leaveout_va_vars_ep1' `grade_vars' `controls_import' ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year



* Test Scores
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore')
}


* Other Outcomes
foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade')
	drop F`grade'
}

foreach grade in  `leaveout_va_vars_ep1'{
	sort studentid year	 
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_ep3') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade')
	drop F`grade'
}

* Jackson Replication
foreach var in `jackson_va_vars'{
	vam `var', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/jack_`var')
}
* Try leaveout approach for test scores
foreach grade in  `other_robustness_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/oth_`grade')
	drop F`grade'
}

* Control for lead and lag teacher FE
gen Fteacherid = F.teacherid
gen Lteacherid = L.teacherid
foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') hdfe(Lteacherid Fteacherid) /// 
		driftlimit(5) output(../data/lausd_data/vascores/bfe_`grade')
	drop F`grade'
}
foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(Fteacherid) /// 
		driftlimit(5) output(../data/lausd_data/vascores/ffe_`grade')
	drop F`grade'
}


drop Fteacherid Lteacherid

* Other Outcomes
foreach grade in  `grade_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_grades') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade')
	drop F`grade'
}

*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end

cap matrix drop varmat

foreach va in `standard_va_vars' `leaveout_va_vars' `leaveout_va_vars_ep1' `grade_vars'{
	insheet using "../data/lausd_data/vascores/`va'_variance.csv", comma clear
	makevarmat
}
	
foreach va in `jackson_va_vars' {
	insheet using "../data/lausd_data/vascores/jack_`va'_variance.csv", comma clear
	makevarmat
}
foreach va in `leaveout_va_vars'{
	insheet using "../data/lausd_data/vascores/bfe_`va'_variance.csv", comma clear
	makevarmat
}
foreach va in `leaveout_va_vars'{
	insheet using "../data/lausd_data/vascores/ffe_`va'_variance.csv", comma clear
	makevarmat
}
foreach va in `other_robustness_vars'{
	insheet using "../data/lausd_data/vascores/oth_`va'_variance.csv", comma clear
	makevarmat
}

* Combine VA data sets
local counter=1
foreach va in `standard_va_vars'{
	if `counter'==1{
		use ../data/lausd_data/vascores/`va', clear
		ren tv tva_`va'
		ren tv_2yr_l tvaqe_`va'
		drop tv_2yr_f tv_ss
	}
	if `counter'>1{
		mmerge teacherid year using ../data/lausd_data/vascores/`va', type(n:1) unmatched(both) ///
			ukeep(tv tv_2yr_l) urename(tv tva_`va' \ tv_2yr_l tvaqe_`va')
	}
	local ++counter
}				

foreach va in `leaveout_va_vars' `leaveout_va_vars_ep1' `grade_vars'{
	mmerge teacherid year using ../data/lausd_data/vascores/`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv tva_`va')

}

foreach va in `jackson_va_vars' {
	mmerge teacherid year using ../data/lausd_data/vascores/jack_`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv j_tva_`va')
}
foreach va in `leaveout_va_vars'{
	mmerge teacherid year using ../data/lausd_data/vascores/bfe_`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv b_tva_`va')	
}
foreach va in `leaveout_va_vars'{
	mmerge teacherid year using ../data/lausd_data/vascores/ffe_`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv f_tva_`va')	
}
foreach va in `other_robustness_vars'{
	mmerge teacherid year using ../data/lausd_data/vascores/oth_`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv oth_tva_`va')	
}

drop _merge

local counter=1

foreach va in `standard_va_vars'{
	gen ztva_`va' = tva_`va'/(varmat[1,`counter']^0.5) 
	gen ztvaqe_`va' = tvaqe_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

foreach va in `leaveout_va_vars' `leaveout_va_vars_ep1' `grade_vars'{
	gen ztva_`va' = tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

foreach va in `jackson_va_vars'{
	gen j_ztva_`va' = j_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}
foreach va in `leaveout_va_vars'{
	di "`counter'"
	di "`va'"
	gen b_ztva_`va' = b_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}
foreach va in `leaveout_va_vars'{
	di "`counter'"
	di "`va'"
	gen f_ztva_`va' = f_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}
foreach va in `other_robustness_vars'{
	gen oth_ztva_`va' = oth_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

drop tva_* j_tva_* oth_tva_* b_tva_* f_tva_* 

saveold ../data/lausd_data/vascores/va_all, replace
************************************************************************
*** Calculate VA Scores - Middle School and High School - JPE R&R  ***
************************************************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "gpatotal logdaysabsent suspended heldback";

local jackson_va_vars	"gpatotal logdaysabsent suspended heldback ";	

* effmarktotalyr effmarktotalyr Leffmarktotalyr Leffmarktotalyr? grade_Leffmarktotalyr class_Leffmarktotalyr class_Leffmarktotalyr? grade_Leffmarktotalyr?;
	
local controls_english "i.grade#C.(Lzcstmath Lzcstela Lgpatotal  Lzcstmath? 
	Lzcstela? Lgpatotal?  Llogdaysabsent Lsuspended Lheldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal  
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?  
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_ell class_size) 
	ell i.grade i.year i.apenglish_classes i.ela_elective i.ela_helper";

local controls_math "i.grade#C.(Lzcstmath Lzcstela Lgpatotal  Lzcstmath? 
	Lzcstela? Lgpatotal?  Llogdaysabsent Lsuspended Lheldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal  
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?  
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_ell class_size) 
	ell i.grade i.year i.trackmath_a i.trackmath_b i.trackmath_c i.trackmath_r i.apmath_classes";
	
#delimit cr;

**Note: Need to include an absorb because the Stata version of VAM is set at 10.1, 
** and in that version reg cannot accept factor variables.

*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end
cap matrix drop varmat

foreach subject in math english{
	
	use ../data/lausd_data/clean_append/student_class_year_all if crstype=="`subject'" & grade>5, clear
	* only use students with one teacher per subject
	duplicates tag studentid year, gen(t)
	drop if t>0
	drop t
	
	xtset studentid year

	* Test Scores
	foreach testscore in `standard_va_vars'{
		vam `testscore', teacher(teacherid) year(year) class(classid) ///
			controls(`controls_`subject'') absorb(year) quasiex /// 
			driftlimit(6) output(../data/lausd_data/vascores/secondary/`testscore'_`subject'_track)
	}


	* Other Outcomes
	foreach grade in  `leaveout_va_vars'{
		vam F`grade', teacher(teacherid) year(year) class(classid) ///
			controls(`controls_`subject'') absorb(year) quasiex /// 
			driftlimit(5) output(../data/lausd_data/vascores/secondary/`grade'_`subject'_track)
	}

	foreach va in `standard_va_vars' `leaveout_va_vars'{
		insheet using "../data/lausd_data/vascores/secondary/`va'_`subject'_variance.csv", comma clear
		makevarmat
	}
		
	
	* Combine VA data sets
	local counter=1
	foreach va in `standard_va_vars'{
		if `counter'==1{
			use ../data/lausd_data/vascores/secondary/`va'_`subject'_track, clear
			ren tv tr_va_`va'
		}
		if `counter'>1{
			mmerge teacherid year using ../data/lausd_data/vascores/secondary/`va'_`subject'_track, type(n:1) unmatched(both) ///
				ukeep(tv) urename(tv tr_va_`va'_`subject')
		}
		local ++counter
	}				

	foreach va in `leaveout_va_vars'{
		mmerge teacherid year using ../data/lausd_data/vascores/secondary/`va'_`subject'_track, type(n:1) unmatched(both) ///
			ukeep(tv) urename(tv tr_va_`va'_`subject')

	}

	cap drop _merge

	local counter=1

	foreach va in `standard_va_vars'{
		gen tr_ztva_`va' = tr_va_`va'/(varmat[1,`counter']^0.5) 
		local ++counter
	}

	foreach va in `leaveout_va_vars'{
		gen tr_ztva_`va' = tr_va_`va'/(varmat[1,`counter']^0.5) 
		local ++counter
	}

	local ++counter

	drop tr_va_* 

	saveold ../data/lausd_data/vascores/secondary/va_secondary_`subject'_track, replace
}

*********************************************************
*** Calculate VA Scores - Elementary School - JPE R&R ***
*********************************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "habmarkfirst5totalyr habmarklast9totalyr gpatotal effmarktotalyr
		logdaysabsent suspended heldback";

local zgpa "zgpatotal zeffmarktotalyr zhabmarkfirst5totalyr zhabmarklast9totalyr";
	
local controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_nolearning "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr  
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

		
local controls_monotonic "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath 
	L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr 
	grade_Lhabmarklast9totalyr grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr
	class_Lhabmarklast9totalyr class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_nonmonotonic "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent? L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent? grade_Lsuspended? grade_Lheldback? grade_Lell?
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent? class_Lsuspended? class_Lheldback? class_Lell? class_size?) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr logdaysabsent zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent? suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent? grade_Lsuspended? grade_Lheldback? grade_Lell?
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent? class_Lsuspended? class_Lheldback? class_Lell?  class_size?
	ell";
	
#delimit cr

use studentid year grade teacherid classid teacherid_grade `standard_va_vars'  ///
	`leaveout_va_vars' `zgpa' `controls_import' ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year

* Test Scores by teacher-grade
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid_grade) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore'tch_grade)
}
* Behavior and Learning Skills VA by teacher-grade
foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid_grade) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade'tch_grade)
	drop F`grade'
}


*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end

cap matrix drop varmat

foreach va in `standard_va_vars' `leaveout_va_vars' {
	insheet using "../data/lausd_data/vascores/`va'tch_grade_variance.csv", comma clear
	makevarmat
}
	
* Combine VA data sets
local counter=1
foreach va in `standard_va_vars' `leaveout_va_vars' {
	if `counter'==1{
		use ../data/lausd_data/vascores/`va'tch_grade, clear
		ren tv tg_tva_`va'
		drop tv_2yr_f tv_ss
	}
	if `counter'>1{
		mmerge teacherid year using ../data/lausd_data/vascores/`va'tch_grade, type(n:1) unmatched(both) ///
			ukeep(tv) urename(tv tg_tva_`va')
	}
	local ++counter
}				

drop _merge

local counter=1

foreach va in `standard_va_vars' `leaveout_va_vars'{
	gen tg_ztva_`va' = tg_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

drop tg_tva_* 
saveold ../data/lausd_data/vascores/va_teacher_grade, replace



use studentid year grade teacherid classid teacherid_grade `standard_va_vars'  ///
	`leaveout_va_vars' `zgpa' `controls_import' ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year

* Main Vars - Monotonic Controls
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_monotonic') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore'mono)
}
foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_monotonic') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade'mono)
	drop F`grade'
}

* Main Vars - Non Monotonic Controls
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_nonmonotonic') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore'nonmono)
}

foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_nonmonotonic') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade'nonmono)
	drop F`grade'
}

* Main Vars - Non Monotonic Controls
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_nonmonotonic') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore'nonmono)
}

foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_nonmonotonic') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade'nonmono)
	drop F`grade'
}

* Main Vars - No learning skills
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_nolearning') absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore'nolearn)
}

foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls_nolearning') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade'nolearn)
	drop F`grade'
}

* Z-score GPA vars
foreach grade in  `zgpa'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade')
	drop F`grade'
}


*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end

cap matrix drop varmat


foreach va in `standard_va_vars' `leaveout_va_vars' {
	insheet using "../data/lausd_data/vascores/`va'mono_variance.csv", comma clear
	makevarmat
}
foreach va in `standard_va_vars' `leaveout_va_vars' {
	insheet using "../data/lausd_data/vascores/`va'nonmono_variance.csv", comma clear
	makevarmat
}
foreach va in `standard_va_vars' `leaveout_va_vars' {
	insheet using "../data/lausd_data/vascores/`va'nolearn_variance.csv", comma clear
	makevarmat
}
foreach va in `zgpa'{
	insheet using "../data/lausd_data/vascores/`va'_variance.csv", comma clear
	makevarmat
}

* Combine VA data sets
local counter=1
foreach va in `standard_va_vars' `leaveout_va_vars' {
	if `counter'==1{
		use ../data/lausd_data/vascores/`va'mono, clear
		ren tv mo_tva_`va'
		drop tv_2yr_f tv_ss
	}
	if `counter'>1{
		mmerge teacherid year using ../data/lausd_data/vascores/`va'mono, type(n:1) unmatched(both) ///
			ukeep(tv) urename(tv mo_tva_`va')
	}
	local ++counter
}				
foreach va in `standard_va_vars' `leaveout_va_vars' {
	mmerge teacherid year using ../data/lausd_data/vascores/`va'nonmono, type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv nmo_tva_`va')
}
foreach va in `standard_va_vars' `leaveout_va_vars' {
	mmerge teacherid year using ../data/lausd_data/vascores/`va'nolearn, type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv nol_tva_`va')
}
foreach va in `zgpa'{
	mmerge teacherid year using ../data/lausd_data/vascores/`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv z_tva_`va')	
}


drop _merge

local counter=1

foreach va in `standard_va_vars' `leaveout_va_vars'{
	gen mo_ztva_`va' = mo_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}
foreach va in `standard_va_vars' `leaveout_va_vars'{
	gen nmo_ztva_`va' = nmo_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}
foreach va in `standard_va_vars' `leaveout_va_vars'{
	gen nol_ztva_`va' = nol_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}
foreach va in `zgpa'{
	di "`counter'"
	di "`va'"
	gen z_ztva_`va' = z_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

drop nmo_tva_* mo_tva_* z_tva_* nol_tva_*

saveold ../data/lausd_data/vascores/va_jpe_robust, replace

************************************************
*** Calculate VA Scores - Chetty Approach    ***
************************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "habmarkfirst5totalyr habmarklast9totalyr gpatotal effmarktotalyr  logdaysabsent suspended heldback";

local jackson_va_vars	"gpatotal logdaysabsent suspended heldback effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr";	

local controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
	
local controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell";
	
#delimit cr

use studentid year grade teacherid classid `standard_va_vars'  ///
	`leaveout_va_vars' `controls_import' ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year

* Test Scores
foreach testscore in `standard_va_vars'{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') tfx_resid(teacherid) /// 
		driftlimit(6) output(../data/lausd_data/vascores/tfe_`testscore')
}


* Other Outcomes
foreach grade in  `leaveout_va_vars'{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') tfx_resid(teacherid) /// 
		driftlimit(5) output(../data/lausd_data/vascores/tfe_`grade')
	drop F`grade'
}


*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end

cap matrix drop varmat

foreach va in `standard_va_vars' `leaveout_va_vars'{
	insheet using "../data/lausd_data/vascores/tfe_`va'_variance.csv", comma clear
	makevarmat
}


* Combine VA data sets
local counter=1
foreach va in `standard_va_vars'{
	if `counter'==1{
		use ../data/lausd_data/vascores/tfe_`va', clear
		ren tv c_tva_`va'
	}
	if `counter'>1{
		mmerge teacherid year using ../data/lausd_data/vascores/tfe_`va', type(n:1) unmatched(both) ///
			ukeep(tv) urename(tv c_tva_`va')
	}
	local ++counter
}				

foreach va in `leaveout_va_vars' {
	mmerge teacherid year using ../data/lausd_data/vascores/tfe_`va', type(n:1) unmatched(both) ///
		ukeep(tv) urename(tv c_tva_`va')

}

drop _merge

local counter=1

foreach va in `standard_va_vars'{
	gen c_ztva_`va' = c_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

foreach va in `leaveout_va_vars' {
	gen c_ztva_`va' = c_tva_`va'/(varmat[1,`counter']^0.5) 
	local ++counter
}

drop c_tva_* 

saveold ../data/lausd_data/vascores/va_chetty, replace


***************************
*** Replicate VA Scores ***
***************************
cap program drop va_rep
program va_rep
	syntax, depvars(string) controls(string) leaveout(string) outputname(string)
	
	* Residualize
	foreach var in `depvars'{
		if upper("`leaveout'")=="NO"{
			reg `var' `controls'
		}
		if upper("`leaveout'")=="YES"{
			reg F.`var' `controls'
		}
		predict `var'_res if e(sample)==1, r
	}

	collapse (mean) *_res (median) grade, by(teacherid year) 
	foreach var in `depvars'{
		* Compute Leaveout Means
		egen tot = total(`var'_res) if `var'_res!=. , by(teacherid)
		egen n   = count(`var'_res) if `var'_res!=. , by(teacherid)
		gen r_ztva_`var'  = (tot-`var'_res)/(n-1)
		egen sd=sd(r_ztva_`var'), by(grade year)
		replace r_ztva_`var'=r_ztva_`var'/sd
		drop `var'_res tot n sd
		
		* Fill in using previous, then next score
		sort teacherid year
		replace r_ztva_`var' = r_ztva_`var'[_n-1] if r_ztva_`var'==. & r_ztva_`var'[_n-1]!=. & teacherid==teacherid[_n-1]
		gsort teacherid -year
		replace r_ztva_`var' = r_ztva_`var'[_n-1] if r_ztva_`var'==. & r_ztva_`var'[_n-1]!=. & teacherid==teacherid[_n-1]
	}
	sort teacherid year
	compress
	saveold ../data/lausd_data/vascores/`outputname', replace
end

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr habmarktotalyr 
	habmarkauthoritytotalyr habmarkcarefultotalyr habmarkcooperatestotalyr habmarkdependabletotalyr 
	habmarkfairplaytotalyr habmarkfollowstotalyr habmarkindependenttotalyr habmarkontimetotalyr 
	habmarkorganizedtotalyr habmarkresolvestotalyr habmarkresponsibletotalyr habmarkselfcontroltotalyr 
	habmarksocialtotalyr habmarkuseoftimetotalyr logdaysabsent suspended heldback";

local controls "i.grade#C.(L.zcstmath L.zcstela L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell";
	
#delimit cr

** Standard Vars
use studentid year grade teacherid classid `standard_va_vars' `controls_import' ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year
va_rep, depvars(`standard_va_vars') controls(`controls') leaveout(no) outputname(standard_va_vars)


use studentid year grade teacherid classid `leaveout_va_vars' `controls_import' ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year
va_rep, depvars(`leaveout_va_vars') controls(`controls') leaveout(yes) outputname(leaveout_va_vars)

use ../data/lausd_data/vascores/leaveout_va_vars, clear
drop if teacherid==.
mmerge teacherid year using ../data/lausd_data/vascores/standard_va_vars, type(1:1) unmatched(both) uif(teacherid!=.)
saveold ../data/lausd_data/vascores/rep_all, replace

****************************************************
*** CREATE FACTOR VARIABLES - MIDDLE/HIGH SCHOOL ***
****************************************************
* Prep so merge is by subject
foreach subject in math english art lang pe science social{
	use ../data/lausd_data/vascores/secondary/va_secondary_`subject'_all, clear
	gen crstype="`subject'"
	saveold ../data/lausd_data/vascores/secondary/va_secondary_`subject'_allcrs, replace
}
foreach subject in math english{
	use ../data/lausd_data/vascores/secondary/va_secondary_`subject'_track, clear
	gen crstype="`subject'"
	saveold ../data/lausd_data/vascores/secondary/va_secondary_`subject'_trackcrs, replace
}

use studentid teacherid grade year crstype num_grades using ../data/lausd_data/clean_append/student_class_year_all if grade>5, clear
merge m:1 teacherid year crstype using ../data/lausd_data/vascores/secondary/va_secondary_math_allcrs, nogen keep(master match)
merge m:1 teacherid year crstype using ../data/lausd_data/vascores/secondary/va_secondary_english_allcrs, nogen keep(1 3 4 5) update
foreach subject in art lang pe science social{
	merge m:1 teacherid year crstype using ../data/lausd_data/vascores/secondary/va_secondary_`subject'_allcrs, ///
	  nogen keep(1 3 4 5) keepusing(ztva_gpa`subject') update
}
merge m:1 teacherid year crstype using ../data/lausd_data/vascores/secondary/va_secondary_math_trackcrs, nogen keep(master match)
merge m:1 teacherid year crstype using ../data/lausd_data/vascores/secondary/va_secondary_english_trackcrs, nogen keep(1 3 4 5) update

* Turn subject vars into one variable
gen ztva_gpa = .
foreach var in ztva_gpamath ztva_gpaenglish ztva_gpaart ztva_gpalang ztva_gpape ztva_gpascience ztva_gpasocial{
	replace ztva_gpa = `var' if `var'!=.
	drop `var'
}

* If student has multiple teacher's in same subject take average of their TVA
collapse (mean) ztva_zcstmath- ztva_gpa [w=num_grades], by(studentid crstype year)
reshape wide ztva_zcstmath- ztva_gpa, i(studentid year) j(crstype) string

merge 1:1 studentid year using ///
	../data/lausd_data/clean_append/student_year_all, /// 
	keep(1 3) keepusing(zcstmath zcstela gpatotal logdaysabsent suspended heldback ) nogen

* Drop vars where all obs are missing
foreach var of varlist _all {
    capture assert mi(`var')
     if !_rc {
        drop `var'
    }
}

matrix LOADS = J(10,2,.)

*** Teacher VA factors 
factor ztva_zcstmathmath ztva_zcstelaenglish
rotate 
predict ztva_cog_factor

mat tempmat = e(r_L)
mat LOADS[1,1] = tempmat[1,1]
mat LOADS[2,1] = tempmat[2,1]

factor ztva_gpatotalenglish ztva_logdaysabsentenglish ztva_suspendedenglish ztva_heldbackenglish ztva_gpatotalmath ///
	ztva_logdaysabsentmath ztva_suspendedmath ztva_heldbackmath
rotate
predict ztva_jackson_factor
replace ztva_jackson_factor=-1*ztva_jackson_factor

mat tempmat = e(r_L)
mat LOADS[3,2] = -1*tempmat[1,1]
mat LOADS[4,2] = -1*tempmat[2,1]
mat LOADS[5,2] = -1*tempmat[3,1]
mat LOADS[6,2] = -1*tempmat[4,1]
mat LOADS[7,2] = -1*tempmat[5,1]
mat LOADS[8,2] = -1*tempmat[6,1]
mat LOADS[9,2] = -1*tempmat[7,1]
mat LOADS[10,2] = -1*tempmat[8,1]

mat list LOADS

**** Create Elementary Loads Table 
cap file close tables 
file open tables using "../output/table40b_hsloads.txt", write replace
file write tables "<tab:hsloads>" _n

file write tables (LOADS[1,1]) _tab _n ///
	(LOADS[2,1]) _tab _n ///
	_tab (LOADS[3,2]) _n ///
	_tab (LOADS[4,2]) _n ///
	_tab (LOADS[5,2]) _n ///
	_tab (LOADS[6,2]) _n ///
	_tab (LOADS[7,2]) _n ///
	_tab (LOADS[8,2]) _n ///
	_tab (LOADS[9,2]) _n ///
	_tab (LOADS[10,2]) _n ///
	
cap file close tables 

**** Various Robustness Checks
**Contemporaneous Jackson Factor
factor  j_ztva_gpatotalenglish j_ztva_logdaysabsentenglish j_ztva_suspendedenglish j_ztva_heldbackenglish j_ztva_gpatotalmath ///
	j_ztva_logdaysabsentmath j_ztva_suspendedmath j_ztva_heldbackmath
rotate 
predict ztva_jackson_factor_contemp
replace ztva_jackson_factor_contemp=-1*ztva_jackson_factor_contemp


**Agnostic Factor Model
factor ztva_zcstelaenglish ztva_zcstmathmath ztva_gpatotalenglish ztva_logdaysabsentenglish ztva_suspendedenglish ztva_heldbackenglish ztva_gpatotalmath ///
	ztva_logdaysabsentmath ztva_suspendedmath ztva_heldbackmath
rotate
predict ztva_agnostic1_factor ztva_agnostic2_factor

*** Even weighted VA factors 
foreach var in ztva_zcstmathmath ztva_zcstelaenglish ztva_gpatotalenglish ztva_logdaysabsentenglish ztva_suspendedenglish ///
	ztva_heldbackenglish ztva_gpatotalmath ztva_logdaysabsentmath ztva_suspendedmath ztva_heldbackmath{
		egen fn_`var' = std(`var')
		egen fn_tr_`var' = std(tr_`var')
}

gen ztva_cog_factor_even = fn_ztva_zcstmathmath + fn_ztva_zcstelaenglish
gen ztva_jackson_factor_even = fn_ztva_gpatotalenglish - fn_ztva_logdaysabsentenglish - fn_ztva_suspendedenglish - fn_ztva_heldbackenglish ///
	+ fn_ztva_gpatotalmath - fn_ztva_logdaysabsentmath - fn_ztva_suspendedmath - fn_ztva_heldbackmath

	
gen ztva_cog_factor_track = fn_tr_ztva_zcstmathmath + fn_tr_ztva_zcstelaenglish
gen ztva_jackson_factor_track = fn_tr_ztva_gpatotalenglish - fn_tr_ztva_logdaysabsentenglish - fn_tr_ztva_suspendedenglish - fn_tr_ztva_heldbackenglish ///
	+ fn_tr_ztva_gpatotalmath - fn_tr_ztva_logdaysabsentmath - fn_tr_ztva_suspendedmath - fn_tr_ztva_heldbackmath

	
	
foreach var in ztva_zcstmathmath ztva_zcstelaenglish ztva_gpatotalenglish ztva_logdaysabsentenglish ztva_suspendedenglish ///
	ztva_heldbackenglish ztva_gpatotalmath ztva_logdaysabsentmath ztva_suspendedmath ztva_heldbackmath {
		drop fn_`var'
		drop fn_tr_`var' 
		
}

*** Rename the factor analysis factors that we orignially used as a robustness factor
rename ztva_cog_factor ztva_cog_factor_orig
rename ztva_jackson_factor ztva_jackson_factor_orig

*** Rename the even weighted factors the main factor variables
rename ztva_cog_factor_even ztva_cog_factor
rename ztva_jackson_factor_even ztva_jackson_factor

*** Even weighted student variable factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback{
		egen fn_`var' = std(`var')
}

gen cog_factor = fn_zcstmath + fn_zcstela
gen jackson_factor = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback

foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback {
		drop fn_`var' 
}
drop zcstmath zcstela gpatotal logdaysabsent suspended heldback

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor*{
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}
compress
saveold ../data/lausd_data/factors/factors_secondary_all, replace



********************************************
*** CREATE FACTOR VARIABLES - ELEMENTARY ***
********************************************
use studentid teacherid grade teacherid_grade year zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr using ../data/lausd_data/clean_append/student_year_all if grade>=2 & grade<=5, clear 
merge m:1 teacherid year using ../data/lausd_data/vascores/rep_all, nogen keep(master match) ///
	keepusing(r_ztva_zcstmath r_ztva_zcstela r_ztva_gpatotal r_ztva_effmarktotalyr r_ztva_habmarkfirst5totalyr r_ztva_habmarklast9totalyr  r_ztva_logdaysabsent r_ztva_suspended r_ztva_heldback)
merge m:1 teacherid year using ../data/lausd_data/vascores/va_all, nogen keep(master match)
merge m:1 teacherid year using ../data/lausd_data/vascores/va_chetty, nogen keep(master match)
merge m:1 teacherid year using ../data/lausd_data/vascores/va_jpe_robust, nogen keep(master match)
merge m:1 teacherid_grade year using ../data/lausd_data/vascores/va_teacher_grade, nogen keep(master match)

cap drop tv_2yr_l

keep if grade>=2 & grade<=5

matrix LOADS = J(9,3,.)

*** Teacher VA factors 
factor ztva_zcstmath ztva_zcstela 
rotate 
predict ztva_cog_factor

mat tempmat = e(r_L)
mat LOADS[1,1] = tempmat[1,1]
mat LOADS[2,1] = tempmat[2,1]

factor ztva_gpatotal ztva_logdaysabsent ztva_suspended ztva_heldback
rotate 
predict ztva_jackson_factor
replace ztva_jackson_factor = -1* ztva_jackson_factor

mat tempmat = e(r_L)
mat LOADS[3,2] = -1*tempmat[1,1]
mat LOADS[4,2] = -1*tempmat[2,1]
mat LOADS[5,2] = -1*tempmat[3,1]
mat LOADS[6,2] = -1*tempmat[4,1]

factor ztva_effmarktotalyr ztva_habmarkfirst5totalyr ztva_habmarklast9totalyr
rotate 
predict ztva_noncog_factor 


mat tempmat = e(L)
mat LOADS[7,3] = tempmat[1,1]
mat LOADS[8,3] = tempmat[2,1]
mat LOADS[9,3] = tempmat[3,1]

mat list LOADS

**** Create Elementary Loads Table 
cap file close tables 
file open tables using "../output/table40a_elemloads.txt", write replace
file write tables "<tab:elemloads>" _n

file write tables (LOADS[1,1]) _tab _tab _n ///
	(LOADS[2,1]) _tab _tab _n ///
	_tab (LOADS[3,2]) _tab _n ///
	_tab (LOADS[4,2]) _tab _n ///
	_tab (LOADS[5,2]) _tab _n ///
	_tab (LOADS[6,2]) _tab _n ///
	_tab _tab (LOADS[7,3]) _n ///
	_tab _tab (LOADS[8,3]) _n ///
	_tab _tab (LOADS[9,3]) _n
cap file close tables 

**** Various Robustness Checks
**Contemporaneous Jackson Factor
factor  j_ztva_gpatotal j_ztva_logdaysabsent j_ztva_suspended j_ztva_heldback
rotate 
predict ztva_jackson_factor_contemp
replace ztva_jackson_factor_contemp = -1* ztva_jackson_factor_contemp

**Contemporaneous NonCog Factor
factor j_ztva_effmarktotalyr j_ztva_habmarkfirst5totalyr j_ztva_habmarklast9totalyr
rotate 
predict ztva_noncog_factor_contemp

**Next Period
factor ztva_effmarktotalyr ztva_habmarkfirst5totalep1 ztva_habmarklast9totalep1
rotate 
predict ztva_noncogep1_factor

**All NonCog
factor ztva_gpatotal ztva_logdaysabsent ztva_suspended ztva_heldback /// 
  ztva_effmarktotalyr ztva_habmarkfirst5totalyr ztva_habmarklast9totalyr  
rotate 
predict ztva_allnoncog_factor if grade>=2 & grade<=5

**Agnostic Factor Model
factor ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_logdaysabsent ztva_suspended ztva_heldback /// 
  ztva_effmarktotalyr ztva_habmarkfirst5totalyr ztva_habmarklast9totalyr  
rotate
predict ztva_agnostic1_factor ztva_agnostic2_factor

matrix LOADS = J(9,2,.)

mat tempmat = e(r_L)
mat LOADS[1,1] = 1*tempmat[1,1]
mat LOADS[2,1] = 1*tempmat[2,1]
mat LOADS[3,1] = 1*tempmat[3,1]
mat LOADS[4,1] = -1*tempmat[4,1]
mat LOADS[5,1] = -1*tempmat[5,1]
mat LOADS[6,1] = -1*tempmat[6,1]
mat LOADS[7,1] = 1*tempmat[7,1]
mat LOADS[8,1] = 1*tempmat[8,1]
mat LOADS[9,1] = 1*tempmat[9,1]

mat LOADS[1,2] = 1*tempmat[1,2]
mat LOADS[2,2] = 1*tempmat[2,2]
mat LOADS[3,2] = 1*tempmat[3,2]
mat LOADS[4,2] = -1*tempmat[4,2]
mat LOADS[5,2] = -1*tempmat[5,2]
mat LOADS[6,2] = -1*tempmat[6,2]
mat LOADS[7,2] = 1*tempmat[7,2]
mat LOADS[8,2] = 1*tempmat[8,2]
mat LOADS[9,2] = 1*tempmat[9,2]

**** Create Agnostic Elementary Loads Table 
cap file close tables 
file open tables using "../output/table40c_elem_agnostic_loads.txt", write replace
file write tables "<tab:elem_agnostic_loads>" _n

file write tables (LOADS[1,1]) _tab (LOADS[1,2]) _n ///
	(LOADS[2,1]) _tab (LOADS[2,2]) _n ///
	(LOADS[3,1]) _tab (LOADS[3,2]) _n ///
	(LOADS[4,1]) _tab (LOADS[4,2]) _n ///
	(LOADS[5,1]) _tab (LOADS[5,2]) _n ///
	(LOADS[6,1]) _tab (LOADS[6,2]) _n ///
	(LOADS[7,1]) _tab (LOADS[7,2]) _n ///
	(LOADS[8,1]) _tab (LOADS[8,2]) _n ///
	(LOADS[9,1]) _tab (LOADS[9,2]) _n

cap file close tables 

**Replication Factors
factor r_ztva_zcstmath r_ztva_zcstela 
rotate 
predict ztva_cog_factor_rep

factor r_ztva_gpatotal r_ztva_logdaysabsent r_ztva_suspended r_ztva_heldback
rotate 
predict ztva_jackson_factor_rep
replace ztva_jackson_factor_rep = -1* ztva_jackson_factor

factor r_ztva_effmarktotalyr r_ztva_habmarkfirst5totalyr r_ztva_habmarklast9totalyr
rotate 
predict ztva_noncog_factor_rep

**Leaveout Test Score
factor oth_ztva_zcstmath oth_ztva_zcstela 
rotate 
predict ztva_cog_factor_leaveout

**Individual GPA Jackson Factor
factor  ztva_logdaysabsent ztva_suspended ztva_heldback ztva_gpamath ztva_gpascience ztva_gpasocial /// 
	ztva_gpalisten ztva_gparead ztva_gpaspeak ztva_gpawrite ztva_gpahealth ztva_gpape ztva_gpaart
rotate 
predict ztva_jackson_factor_all1 ztva_jackson_factor_all2


**Individual Habits Non-Cog Factor
factor ztva_effmarktotalyr ztva_habmarkauthoritytotalyr ztva_habmarkcarefultotalyr ztva_habmarkcooperatestotalyr ///
 ztva_habmarkdependabletotalyr ztva_habmarkfairplaytotalyr ztva_habmarkfollowstotalyr ztva_habmarkindependenttotalyr /// 
 ztva_habmarkontimetotalyr ztva_habmarkorganizedtotalyr ztva_habmarkresolvestotalyr ztva_habmarkresponsibletotalyr /// 
 ztva_habmarkselfcontroltotalyr 
rotate 
predict ztva_noncog_factor_all1 ztva_noncog_factor_all2 ztva_noncog_factor_all3

**Agnostic Factor Model - All Measures
factor ztva_zcstmath ztva_zcstela ztva_logdaysabsent ztva_suspended ztva_heldback ztva_effmarktotalyr ///
 ztva_habmarkauthoritytotalyr ztva_habmarkcarefultotalyr ztva_habmarkcooperatestotalyr ///
 ztva_habmarkdependabletotalyr ztva_habmarkfairplaytotalyr ztva_habmarkfollowstotalyr ztva_habmarkindependenttotalyr /// 
 ztva_habmarkontimetotalyr ztva_habmarkorganizedtotalyr ztva_habmarkresolvestotalyr ztva_habmarkresponsibletotalyr /// 
 ztva_habmarkselfcontroltotalyr ztva_gpamath ztva_gpascience ztva_gpasocial /// 
 ztva_gpalisten ztva_gparead ztva_gpaspeak ztva_gpawrite ztva_gpahealth ztva_gpape ztva_gpaart
 
rotate
predict ztva_agnostic1_factor_all ztva_agnostic2_factor_all ztva_agnostic3_factor_all ztva_agnostic4_factor_all

*** Even weighted VA factors 
foreach var in ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_logdaysabsent ztva_suspended ztva_heldback ///
	ztva_effmarktotalyr ztva_habmarkfirst5totalyr ztva_habmarklast9totalyr c_ztva_zcstmath ///
	c_ztva_zcstela c_ztva_habmarkfirst5totalyr c_ztva_habmarklast9totalyr c_ztva_gpatotal /// 
	c_ztva_effmarktotalyr c_ztva_logdaysabsent c_ztva_suspended c_ztva_heldback ///
	b_ztva_gpatotal b_ztva_effmarktotalyr b_ztva_logdaysabsent b_ztva_suspended b_ztva_heldback ///
	b_ztva_habmarkfirst5totalyr b_ztva_habmarklast9totalyr mo_ztva_zcstmath /// 
	mo_ztva_zcstela mo_ztva_habmarkfirst5totalyr mo_ztva_habmarklast9totalyr mo_ztva_gpatotal /// 
	mo_ztva_effmarktotalyr mo_ztva_logdaysabsent mo_ztva_suspended mo_ztva_heldback nmo_ztva_zcstmath ///
	nmo_ztva_zcstela nmo_ztva_habmarkfirst5totalyr nmo_ztva_habmarklast9totalyr nmo_ztva_gpatotal /// 
	nmo_ztva_effmarktotalyr nmo_ztva_logdaysabsent nmo_ztva_suspended nmo_ztva_heldback /// 
	nol_ztva_zcstmath nol_ztva_zcstela nol_ztva_habmarkfirst5totalyr nol_ztva_habmarklast9totalyr /// 
	nol_ztva_gpatotal nol_ztva_effmarktotalyr nol_ztva_logdaysabsent nol_ztva_suspended /// 
	nol_ztva_heldback z_ztva_zgpatotal z_ztva_zeffmarktotalyr z_ztva_zhabmarkfirst5totalyr /// 
	z_ztva_zhabmarklast9totalyr tg_ztva_zcstmath tg_ztva_zcstela tg_ztva_habmarkfirst5totalyr /// 
	tg_ztva_habmarklast9totalyr tg_ztva_gpatotal tg_ztva_effmarktotalyr tg_ztva_logdaysabsent /// 
	tg_ztva_suspended tg_ztva_heldback	j_ztva_gpatotal j_ztva_logdaysabsent j_ztva_suspended /// 
	j_ztva_heldback j_ztva_effmarktotalyr j_ztva_habmarkfirst5totalyr j_ztva_habmarklast9totalyr{
		egen fn_`var' = std(`var')
}

*
gen ztva_cog_factor_even = fn_ztva_zcstmath + fn_ztva_zcstela
gen ztva_jackson_factor_even = fn_ztva_gpatotal - fn_ztva_logdaysabsent - fn_ztva_suspended - fn_ztva_heldback
gen ztva_noncog_factor_even = fn_ztva_effmarktotalyr + fn_ztva_habmarkfirst5totalyr + fn_ztva_habmarklast9totalyr

* Alternative Split
gen ztva_inschool_factor = -1*fn_ztva_logdaysabsent - fn_ztva_suspended - fn_ztva_heldback
gen ztva_allgpas_factor = fn_ztva_gpatotal + fn_ztva_effmarktotalyr + fn_ztva_habmarkfirst5totalyr + fn_ztva_habmarklast9totalyr
gen ztva_allcog_factor = fn_ztva_zcstmath + fn_ztva_zcstela + fn_ztva_gpatotal 

* No Effort
gen ztva_habits_factor = fn_ztva_habmarkfirst5totalyr + fn_ztva_habmarklast9totalyr
	
* Chetty Replication
gen ztva_cog_factor_chetty = fn_c_ztva_zcstmath + fn_c_ztva_zcstela
gen ztva_jackson_factor_chetty = fn_c_ztva_gpatotal - fn_c_ztva_logdaysabsent - fn_c_ztva_suspended - fn_c_ztva_heldback
gen ztva_noncog_factor_chetty = fn_c_ztva_effmarktotalyr + fn_c_ztva_habmarkfirst5totalyr + fn_c_ztva_habmarklast9totalyr

* Estimated with Fixed Effects
gen ztva_jackson_factor_wfe = fn_b_ztva_gpatotal - fn_b_ztva_logdaysabsent - fn_b_ztva_suspended - fn_b_ztva_heldback
gen ztva_noncog_factor_wfe  = fn_b_ztva_effmarktotalyr + fn_b_ztva_habmarkfirst5totalyr + fn_b_ztva_habmarklast9totalyr

* Monotonic Controls
gen ztva_cog_factor_mono = fn_mo_ztva_zcstmath + fn_mo_ztva_zcstela
gen ztva_jackson_factor_mono = fn_mo_ztva_gpatotal - fn_mo_ztva_logdaysabsent - fn_mo_ztva_suspended - fn_mo_ztva_heldback
gen ztva_noncog_factor_mono = fn_mo_ztva_effmarktotalyr + fn_mo_ztva_habmarkfirst5totalyr + fn_mo_ztva_habmarklast9totalyr

* Non-Monotonic Controls
gen ztva_cog_factor_nomono = fn_nmo_ztva_zcstmath + fn_nmo_ztva_zcstela
gen ztva_jackson_factor_nomono = fn_nmo_ztva_gpatotal - fn_nmo_ztva_logdaysabsent - fn_nmo_ztva_suspended - fn_nmo_ztva_heldback
gen ztva_noncog_factor_nomono = fn_nmo_ztva_effmarktotalyr + fn_nmo_ztva_habmarkfirst5totalyr + fn_nmo_ztva_habmarklast9totalyr

* Year t Outcomes
gen ztva_jackson_factor_yrt = fn_j_ztva_gpatotal - fn_j_ztva_logdaysabsent - fn_j_ztva_suspended - fn_j_ztva_heldback
gen ztva_noncog_factor_yrt = fn_j_ztva_effmarktotalyr + fn_j_ztva_habmarkfirst5totalyr + fn_j_ztva_habmarklast9totalyr

* Z Scored GPA
gen ztva_jackson_factor_zgpa = fn_z_ztva_zgpatotal - fn_ztva_logdaysabsent - fn_ztva_suspended - fn_ztva_heldback
gen ztva_noncog_factor_zgpa = fn_z_ztva_zeffmarktotalyr + fn_z_ztva_zhabmarkfirst5totalyr + fn_z_ztva_zhabmarklast9totalyr

* Teacher-Grade VA Measures
gen ztva_cog_factor_tchgrade = fn_tg_ztva_zcstmath + fn_tg_ztva_zcstela
gen ztva_jackson_factor_tchgrade = fn_tg_ztva_gpatotal - fn_tg_ztva_logdaysabsent - fn_tg_ztva_suspended - fn_tg_ztva_heldback
gen ztva_noncog_factor_tchgrade = fn_tg_ztva_effmarktotalyr + fn_tg_ztva_habmarkfirst5totalyr + fn_tg_ztva_habmarklast9totalyr

* Estimates Excluding Learning Skills Controls
gen ztva_cog_factor_nolrnctrl = fn_nol_ztva_zcstmath + fn_nol_ztva_zcstela
gen ztva_jackson_factor_nolrnctrl = fn_nol_ztva_gpatotal - fn_nol_ztva_logdaysabsent - fn_nol_ztva_suspended - fn_nol_ztva_heldback
gen ztva_noncog_factor_nolrnctrl = fn_nol_ztva_effmarktotalyr + fn_nol_ztva_habmarkfirst5totalyr + fn_nol_ztva_habmarklast9totalyr

* Drop variables we don't need
drop fn_*
drop mo_ztva_habmarklast9totalyr mo_ztva_gpatotal ///
	mo_ztva_effmarktotalyr mo_ztva_logdaysabsent mo_ztva_suspended mo_ztva_heldback nmo_ztva_zcstmath ///
	nmo_ztva_zcstela nmo_ztva_habmarkfirst5totalyr nmo_ztva_habmarklast9totalyr nmo_ztva_gpatotal /// 
	nmo_ztva_effmarktotalyr nmo_ztva_logdaysabsent nmo_ztva_suspended nmo_ztva_heldback /// 
	nol_ztva_zcstmath nol_ztva_zcstela nol_ztva_habmarkfirst5totalyr nol_ztva_habmarklast9totalyr /// 
	nol_ztva_gpatotal nol_ztva_effmarktotalyr nol_ztva_logdaysabsent nol_ztva_suspended /// 
	nol_ztva_heldback z_ztva_zgpatotal z_ztva_zeffmarktotalyr z_ztva_zhabmarkfirst5totalyr /// 
	z_ztva_zhabmarklast9totalyr tg_ztva_zcstmath tg_ztva_zcstela tg_ztva_habmarkfirst5totalyr /// 
	tg_ztva_habmarklast9totalyr tg_ztva_gpatotal tg_ztva_effmarktotalyr tg_ztva_logdaysabsent /// 
	tg_ztva_suspended tg_ztva_heldback

*** Rename the factor analysis factors that we orignially used as a robustness factor
rename ztva_cog_factor ztva_cog_factor_orig
rename ztva_jackson_factor ztva_jackson_factor_orig
rename ztva_noncog_factor ztva_noncog_factor_orig

*** Rename the even weighted factors the main factor variables
rename ztva_cog_factor_even ztva_cog_factor
rename ztva_jackson_factor_even ztva_jackson_factor
rename ztva_noncog_factor_even ztva_noncog_factor


*** Student variable factors 
factor zcstmath zcstela  
rotate 
predict cog_factor_orig 

factor gpatotal logdaysabsent suspended heldback if grade>=2 & grade<=5
rotate 
predict jackson_factor_orig
replace jackson_factor_orig = -1*jackson_factor_orig

factor effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr
rotate 
predict noncog_factor_orig

*** Even weighted student variable factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback effmarktotalyr ///
	habmarkfirst5totalyr habmarklast9totalyr oth_ztva_zcstmath oth_ztva_zcstela{
		egen fn_`var' = std(`var')
}

gen cog_factor = fn_zcstmath + fn_zcstela
gen jackson_factor = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback
gen noncog_factor = fn_effmarktotalyr + fn_habmarkfirst5totalyr + fn_habmarklast9totalyr

gen ztva_fcog_factor=fn_oth_ztva_zcstmath+fn_oth_ztva_zcstela

drop fn_*

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor*{
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}
compress
saveold ../data/lausd_data/factors/factors_all, replace



*******************************************
*** Calculate Value Added using factors ***
*******************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell";
	

	
#delimit cr

use studentid year grade teacherid classid `controls_import' using ../data/lausd_data/clean_append/student_year_all, clear 
merge m:1 studentid year using ../data/lausd_data/factors/factors_all, nogen keepusing(cog_factor jackson_factor noncog_factor) keep(match)
xtset studentid year
* Test Scores
foreach testscore in cog_factor{
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasie /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore')
}


* Other Outcomes
foreach grade in  jackson_factor noncog_factor{
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls(`controls') absorb(year) quasie /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade')
	drop F`grade'
}


*** Create matrix with (1) covariance between t and t+1, (2) total variance, (3) class variance, /// 
***                    (4) individual variance (i.e. within class variance)
cap program drop makevarmat
program makevarmat
	sum lag 
	matrix varmat = (nullmat(varmat) , (cov_lag[1] \ var_total[r(N)+1] \ var_class[r(N)+1] \ var_ind[r(N)+1] ))
end

preserve
cap matrix drop varmat

foreach va in cog_factor jackson_factor noncog_factor{
	insheet using "../data/lausd_data/vascores/`va'_variance.csv", comma clear
	makevarmat
}


* Combine VA data sets
local counter=1
foreach va in  cog_factor jackson_factor noncog_factor{
	if `counter'==1{
		use ../data/lausd_data/vascores/`va', clear
		ren tv tva_`va'_post
		ren tv_2yr_l tva2l_`va'_post
		ren tv_2yr_f tva2f_`va'_post
		drop tv_ss
	}
	if `counter'>1{
		mmerge teacherid year using ../data/lausd_data/vascores/`va', type(n:1) unmatched(both) ukeep(tv tv_2yr_l tv_2yr_f) ///
			urename(tv tva_`va'_post \ tv_2yr_l tva2l_`va'_post \ tv_2yr_f tva2f_`va'_post) 
	}
	local ++counter
}				
drop _merge

local counter=1

foreach va in cog_factor jackson_factor noncog_factor{
	gen ztva_`va'_post   = tva_`va'_post/(varmat[1,`counter']^0.5) 
	gen ztva2f_`va'_post = tva2f_`va'_post/(varmat[1,`counter']^0.5)
	gen ztva2l_`va'_post = tva2l_`va'_post/(varmat[1,`counter']^0.5)
	local ++counter
}
*drop tva_*
saveold ../data/lausd_data/vascores/post_factors, replace

********************************
*** Create Analysis Data Set ***
********************************
*** Teacher Level data set 
use ../data/lausd_data/vascores/post_factors,  clear
merge m:1 teacherid year using ../data/lausd_data/vascores/rep_all, nogen ///
	keepusing(r_ztva_zcstmath r_ztva_zcstela r_ztva_gpatotal r_ztva_effmarktotalyr r_ztva_habmarkfirst5totalyr r_ztva_habmarklast9totalyr  r_ztva_logdaysabsent r_ztva_suspended r_ztva_heldback)
merge m:1 teacherid year using ../data/lausd_data/vascores/va_all, nogen
compress
saveold ../data/lausd_data/temp/teacherid_analysis, replace

*** Student-Level data set
use ../data/lausd_data/clean_append/student_year_all, clear 
drop L* F* 
merge m:1 studentid year using ../data/lausd_data/factors/factors_all, keep(match master) nogen
merge 1:1 studentid year using ../data/lausd_data/factors/factors_secondary_all, keep(1 3 4 5) nogen update
merge m:1 teacherid year using ../data/lausd_data/temp/teacherid_analysis, keep(match master) nogen
merge m:1 cdscode using ../data/lausd_data/temp/cdscode_hscdscode, keep(match master) nogen
replace hs_cdscode=cdscode if grade >=9 & grade<=12 & cdscode!=.

* Replace predicted high school CDS code with actual if predicted is missing
* Pick cdscode arbitraryily if there are muliple ones
forvalues grade=9/12{
	egen cdscode`grade'=max(cdscode*(grade==`grade')), by(studentid)
	replace hs_cdscode = cdscode`grade' if cdscode`grade'!=. & cdscode`grade'!= 0 & hs_cdscode==.
}
drop cdscode9-cdscode12

*** Create school-cohort for clustering.  
gen kinderyear = year - grade  
egen cohortsch = group(cdscode kinderyear)

cap drop zttva_*
compress
saveold ../data/lausd_data/analysis/student_year_main, replace



**************************************************
*** CREATE DATA SET FOR ELEMENTARY FORECAST BIAS ANALYSIS ***
**************************************************
adopath ++ ./Chetty/adopath/

#delimit ;

global standard_va_vars "zcstmath zcstela";

global leaveout_va_vars "habmarkfirst5totalyr habmarklast9totalyr habmarktotalyr 
	habmarkauthoritytotalyr habmarkcarefultotalyr habmarkcooperatestotalyr habmarkdependabletotalyr 
	habmarkfairplaytotalyr habmarkfollowstotalyr habmarkindependenttotalyr habmarkontimetotalyr 
	habmarkorganizedtotalyr habmarkresolvestotalyr habmarkresponsibletotalyr habmarkselfcontroltotalyr 
	habmarksocialtotalyr habmarkuseoftimetotalyr gpatotal effmarktotalyr  logdaysabsent suspended heldback";

global leaveout_va_vars_ep1 "habmarkfirst5totalep1 habmarklast9totalep1 habmarktotalep1";	

global other_robustness_vars "zcstmath zcstela";

global grade_vars "gpaenglish gpamath gpascience gpasocial gpalisten gparead gpaspeak gpawrite gpahealth gpape gpaart";
	
global controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
	
global controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size
	habmarkfirst5totalep3 habmarklast9totalep3 habmarkfirst5totalep3? habmarklast9totalep3? 
	grade_Lhabmarkfirst5totalep3 grade_Lhabmarklast9totalep3 grade_Lhabmarkfirst5totalep3? 
	grade_Lhabmarklast9totalep3? class_Lhabmarkfirst5totalep3 class_Lhabmarklast9totalep3 class_Lhabmarkfirst5totalep3? 
	class_Lhabmarklast9totalep3? ell";
	
#delimit cr

use studentid year grade teacherid classid cdscode $standard_va_vars  ///
	$leaveout_va_vars $controls_import ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year

reg zcstmath zcstela $controls
gen cool = (e(sample) == 1)

* Test Scores
foreach testscore in zcstmath zcstela {
	xtset studentid year
	vam `testscore', teacher(teacherid) year(year) class(classid) ///
		controls($controls) absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/`testscore') data(merge tv score_r)
	rename tv tv_`testscore'
	rename score_r score_r_`testscore'
	rename tv_2yr_l tv_2yr_l_`testscore'	
	rename tv_2yr_f tv_2yr_f_`testscore'
	rename tv_ss tv_ss_`testscore'
}

* Other Outcomes
foreach grade in habmarktotalyr habmarkfirst5totalyr habmarklast9totalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	xtset studentid year
	gen F`grade'=F.`grade'
	vam F`grade', teacher(teacherid) year(year) class(classid) ///
		controls($controls) absorb(year) quasiex /// 
		driftlimit(5) output(../data/lausd_data/vascores/`grade') data(merge tv score_r)
	drop F`grade'
	rename tv tv_`grade'
	rename score_r score_r_`grade'
	rename tv_2yr_l tv_2yr_l_`grade'	
	rename tv_2yr_f tv_2yr_f_`grade'
	rename tv_ss tv_ss_`grade'
}

* Jackson Replication
foreach var in habmarktotalyr habmarkfirst5totalyr habmarklast9totalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	xtset studentid year
	vam `var', teacher(teacherid) year(year) class(classid) ///
		controls($controls) absorb(year) quasiex /// 
		driftlimit(6) output(../data/lausd_data/vascores/jack_`var') data(merge tv score_r)
	rename tv tv_jack`var'
	rename score_r score_r_jack`var'
	if "`var'"!="habmarkfirst5totalyr" & "`var'"!="habmarklast9totalyr"{
		rename tv_2yr_l tv_2yr_l_jack`var'	
		rename tv_2yr_f tv_2yr_f_jack`var'
		rename tv_ss tv_ss_jack`var'
	}
	else{
		drop tv_2yr_l tv_2yr_f tv_ss 
	}
}

* Add HS Clusters
merge 1:1 studentid year using ../data/lausd_data/analysis/student_year_main, keep(1 3) keepusing(hs_cdscode) nogen

compress
saveold ../data/lausd_data/vascores/biastest_studentlevel, replace



* Create residualized two year lagged variables using the baseline controls

use studentid year grade teacherid classid cdscode $standard_va_vars  ///
	$leaveout_va_vars $controls_import ///
	 using ../data/lausd_data/clean_append/student_year_all if grade>=2 &  grade<=5, clear
xtset studentid year

gen tempscore = . 
foreach var in zcstmath zcstela habmarktotalyr habmarkfirst5totalyr habmarklast9totalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	xtset studentid year
	replace tempscore = . 
	replace tempscore = L2.`var'
	vam tempscore, teacher(teacherid) year(year) class(classid) ///
		controls($controls) absorb(year) /// 
		driftlimit(6) output(../data/lausd_data/vascores/residl2var_`var') data(merge score_r)
	rename score_r l2rscore_`var'

}

keep year studentid l2rscore_zcstmath l2rscore_zcstela l2rscore_habmarktotalyr l2rscore_habmarkfirst5totalyr l2rscore_habmarklast9totalyr ///
l2rscore_gpatotal l2rscore_effmarktotalyr l2rscore_logdaysabsent l2rscore_suspended l2rscore_heldback

* Add HS Clusters
merge 1:1 studentid year using ../data/lausd_data/analysis/student_year_main, keep(1 3) keepusing(hs_cdscode) nogen

compress
saveold ../data/lausd_data/vascores/twoyrlag_residvariables, replace

***********************************************************************
*** Create Highschool Autocorrelation and Forcastbias test data set ***
***********************************************************************
adopath ++ ./Chetty/adopath/

#delimit ;
local standard_va_vars "zcstmath zcstela";

local leaveout_va_vars "gpatotal logdaysabsent suspended heldback";

local jackson_va_vars	"gpatotal logdaysabsent suspended heldback ";	
* effmarktotalyr effmarktotalyr Leffmarktotalyr Leffmarktotalyr? grade_Leffmarktotalyr class_Leffmarktotalyr class_Leffmarktotalyr? grade_Leffmarktotalyr?;
	
local controls "i.grade#C.(Lzcstmath Lzcstela Lgpatotal  Lzcstmath? 
	Lzcstela? Lgpatotal?  Llogdaysabsent Lsuspended Lheldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal  
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?  
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_ell class_size) 
	ell i.grade i.year";

#delimit cr;

foreach subject in math english{
	use ../data/lausd_data/clean_append/student_class_year_all if crstype=="`subject'" & grade>5, clear
	* only use students with one teacher per subject
	duplicates tag studentid year, gen(t)
	drop if t>0
	drop t
	
	xtset studentid year

	* Test Scores
	foreach testscore in `standard_va_vars'{
		vam `testscore', teacher(teacherid) year(year) class(classid) ///
			controls(`controls') absorb(year) quasiex /// 
			driftlimit(6) data(merge tv score_r)
		rename tv tv_`testscore'_`subject'
		rename score_r score_r_`testscore'_`subject'
		rename tv_2yr_l tv_2yr_l_`testscore'_`subject'	
		rename tv_2yr_f tv_2yr_f_`testscore'_`subject'
		rename tv_ss tv_ss_`testscore'_`subject'
	
	}

	* Other Outcomes
	foreach grade in  `leaveout_va_vars'{
		vam F`grade', teacher(teacherid) year(year) class(classid) ///
			controls(`controls') absorb(year) quasiex /// 
			driftlimit(5) data(merge tv score_r)
		rename tv tv_`grade'_`subject'
		rename score_r score_r_`grade'_`subject'
		rename tv_2yr_l tv_2yr_l_`grade'_`subject'	
		rename tv_2yr_f tv_2yr_f_`grade'_`subject'
		rename tv_ss tv_ss_`grade'_`subject'
	}

	* Add HS Clusters
	merge 1:1 studentid year using ../data/lausd_data/analysis/student_year_main, keep(1 3) keepusing(hs_cdscode) nogen
	
	sort studentid year 
	compress
	saveold ../data/lausd_data/vascores/hbiastest_studentlevel_HS_`subject', replace
}

****************************************************************************
*** Create data for TABLE 20h: Testing Bias School-Grade Level Variation ***
****************************************************************************
use ../data/lausd_data/analysis/student_year_main if grade >= 2 & grade <= 5, clear
xtset studentid year
adopath ++ ./Chetty/adopath/
#delimit ;

local controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
	
#delimit cr;

keep if teacherid != . 
egen tchyrcount = count(zcstmath), by(teacherid year)
keep if tchyrcount > 10 & tchyrcount < 45

*areg zcstmath `controls', a(teacherid)
*gen sampmath = (e(sample) == 1)

	* Test Scores
	foreach testscore in zcstmath zcstela gpatotal logdaysabsent suspended heldback habmarktotalyr effmarktotalyr {
		xtset studentid year
		vam `testscore', teacher(teacherid) year(year) class(classid) ///
			controls(`controls') absorb(year) quasiex /// 
			driftlimit(6) data(merge tv score_r)
		rename tv tv_`testscore'
		rename score_r score_r_`testscore'
		rename tv_2yr_l tv_2yr_l_`testscore'	
		rename tv_2yr_f tv_2yr_f_`testscore'
		rename tv_ss tv_ss_`testscore'
	
	}
merge 1:1 studentid year using ../data/lausd_data/analysis/student_year_main, keep(1 3) keepusing(hs_cdscode) nogen
compress
save "../data/lausd_data/temp/tempdata_school_bias_test.dta", replace


	
***************
*** Results ***
***************
cap log close
log using ../output/results.log, replace
adopath + ./Other_ado/
adopath + ./Other_ado/plus
use ../data/lausd_data/analysis/student_year_main, clear
drop b_ztva* j_ztva* f_ztva* oth_ztva* c_ztva* tr_ztva*
xtset studentid year

set matsize 11000


fvset base 2007 year
fvset base 3 grade


#delimit ;
local standard_va_vars "zcstmath zcstela";


local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local ctrls_split1 "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	ell i.grade i.year";
	
local ctrls_split2 "i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size)";

local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

reg hs_logdaysabsent ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)

***********************************
*** TABLE 1: Summary Statistics ***
***********************************
cap file close tables 
file open tables using "../output/table1_sum.txt", write replace
file write tables "<tab:sumstats>" _n

*Focus on sample where we have data on the individuals for summary stats and correlations
preserve
keep if zcstmath != . | zcstela ! = . | gpatotal != . 

foreach var in zcstmath zcstela gpatotal effmarktotalyr habmarktotalyr fracdaysabsent dayssuspended heldback ell { 
	if "`var'" == "fracdaysabsent" | "`var'" == "heldback" | "`var'" == "ell" {
		sum `var' if (grade >= 3 & grade <= 5) & (year >= 2004 & year <= 2010)
		file write tables %9.7f (100*r(mean)) _tab "." _tab %9.0f (r(N)) _n
	}
	else {
		sum `var' if (grade >= 3 & grade <= 5) & (year >= 2004 & year <= 2010)
		file write tables (r(mean)) _tab (r(sd)) _tab (r(N)) _n
	}
}

foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal zcahseescoremath ///
	zcahseescoreela hs_dayssuspended hs_fracdaysabsent hs_heldback evergrad everpsat psatscoreoverall ///
	F7.zcstmath F7.zcstela zcstscience8 zcstscience10 zcstsocial8 zcstsocial11 zcstsocialworld apclasstotal {
		if "`var'" == "hs_dropout" | "`var'" == "eversat" | "`var'" == "hs_fracdaysabsent" | ///
		"`var'" == "hs_heldback" | "`var'" == "evergrad" | "`var'" == "everpsat" {
			sum `var' if (grade >= 3 & grade <= 5) & (year >= 2004 & year <= 2010)
			file write tables %9.7f (100*r(mean)) _tab "." _tab %9.0f (r(N)) _n
		}
		else { 
			sum `var' if (grade >= 3 & grade <= 5) & (year >= 2004 & year <= 2010)
			file write tables %9.7f (r(mean)) _tab %9.7f (r(sd)) _tab %9.0f (r(N)) _n
		}
}

cap file close tables

******************************************************
*** TABLE 2a: VA Correlations using all VA measures ***
******************************************************
corr ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_habmarktotalyr ztva_effmarktotalyr ztva_logdaysabsent ztva_suspended ztva_heldback /// 
	if grade >= 3 & grade <= 5
matrix corrbig = r(C)

cap file close tables 
file open tables using "../output/table2_corrbig.txt", write replace
file write tables "<tab:corrbig>" _n

file write tables (corrbig[1,1]) _n ///
	(corrbig[2,1]) _tab (corrbig[2,2]) _n ///
	(corrbig[3,1]) _tab (corrbig[3,2]) _tab (corrbig[3,3]) _n ///
	(corrbig[4,1]) _tab (corrbig[4,2]) _tab (corrbig[4,3]) _tab (corrbig[4,4]) _n ///
	(corrbig[5,1]) _tab (corrbig[5,2]) _tab (corrbig[5,3]) _tab (corrbig[5,4]) _tab (corrbig[5,5]) _n ///
	(corrbig[6,1]) _tab (corrbig[6,2]) _tab (corrbig[6,3]) _tab (corrbig[6,4]) _tab (corrbig[6,5]) _tab (corrbig[6,6]) _n ///
	(corrbig[7,1]) _tab (corrbig[7,2]) _tab (corrbig[7,3]) _tab (corrbig[7,4]) _tab (corrbig[7,5]) _tab (corrbig[7,6]) _tab (corrbig[7,7]) _n ///
 	(corrbig[8,1]) _tab (corrbig[8,2]) _tab (corrbig[8,3]) _tab (corrbig[8,4]) _tab (corrbig[8,5]) _tab (corrbig[8,6]) _tab (corrbig[8,7]) _tab (corrbig[8,8]) _n
 
cap file close tables 

******************************************************
*** TABLE 2b: Non-VA Correlations using all VA measures ***
******************************************************
corr zcstmath zcstela gpatotal habmarktotalyr effmarktotalyr logdaysabsent suspended heldback /// 
	if grade >= 3 & grade <= 5
matrix corrbig = r(C)

cap file close tables 
file open tables using "../output/table2_nonvacorrbig.txt", write replace
file write tables "<tab:nonvacorrbig>" _n

file write tables (corrbig[1,1]) _n ///
	(corrbig[2,1]) _tab (corrbig[2,2]) _n ///
	(corrbig[3,1]) _tab (corrbig[3,2]) _tab (corrbig[3,3]) _n ///
	(corrbig[4,1]) _tab (corrbig[4,2]) _tab (corrbig[4,3]) _tab (corrbig[4,4]) _n ///
	(corrbig[5,1]) _tab (corrbig[5,2]) _tab (corrbig[5,3]) _tab (corrbig[5,4]) _tab (corrbig[5,5]) _n ///
	(corrbig[6,1]) _tab (corrbig[6,2]) _tab (corrbig[6,3]) _tab (corrbig[6,4]) _tab (corrbig[6,5]) _tab (corrbig[6,6]) _n ///
	(corrbig[7,1]) _tab (corrbig[7,2]) _tab (corrbig[7,3]) _tab (corrbig[7,4]) _tab (corrbig[7,5]) _tab (corrbig[7,6]) _tab (corrbig[7,7]) _n ///
 	(corrbig[8,1]) _tab (corrbig[8,2]) _tab (corrbig[8,3]) _tab (corrbig[8,4]) _tab (corrbig[8,5]) _tab (corrbig[8,6]) _tab (corrbig[8,7]) _tab (corrbig[8,8]) _n
 
cap file close tables 

*********************************************************
*** TABLE 3a: VA Correlations using 3 factor variables ***
*********************************************************
corr ztva_cog_factor ztva_jackson_factor ztva_noncog_factor if grade >= 3 & grade <= 5
matrix corrsmall = r(C)

cap file close tables 
file open tables using "../output/table3_corrsmall.txt", write replace
file write tables "<tab:corrsmall>" _n

file write tables (corrsmall[1,1]) _n ///
	(corrsmall[2,1]) _tab (corrsmall[2,2]) _n ///
	(corrsmall[3,1]) _tab (corrsmall[3,2]) _tab (corrsmall[3,3]) _n

cap file close tables

*********************************************************
*** TABLE 3b: Non-VA Correlations using 3 factor variables ***
*********************************************************
corr cog_factor jackson_factor noncog_factor if grade >= 3 & grade <= 5
matrix corrsmall = r(C)

cap file close tables 
file open tables using "../output/table3_nonvacorrsmall.txt", write replace
file write tables "<tab:nonvacorrsmall>" _n

file write tables (corrsmall[1,1]) _n ///
	(corrsmall[2,1]) _tab (corrsmall[2,2]) _n ///
	(corrsmall[3,1]) _tab (corrsmall[3,2]) _tab (corrsmall[3,3]) _n

cap file close tables

* Switch back to fulls sample for the analysis
restore

************************************************
*** TABLE 4: Predictions using all variables ***
************************************************
cap file close tables 
file open tables using "../output/table4_predictbig.txt", write replace
file write tables "<tab:predictbig>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 

	reg `var' zcstmath zcstela zgpatotal zhabmarktotalyr zeffmarktotalyr suspended logdaysabsent heldback `ctrls' /// 
	  if grade >= 3 & grade <= 5, cluster(hs_cdscode)

	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table4_predictbig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(zcstmath zcstela zgpatotal zhabmarktotalyr zeffmarktotalyr suspended logdaysabsent heldback)


*****************************************************
*** TABLE 5: Predictions using 3 factor variables ***
*****************************************************

cap file close tables 
file open tables using "../output/table5_predictsmall.txt", write replace
file write tables "<tab:predictsmall>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' cog_factor jackson_factor noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode) 
	estimate store model`counter'	
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table5_predictsmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(cog_factor jackson_factor noncog_factor)


**********************************************************************
*** TABLE 5b: Predictions using 3 factor variables - Other Outcomes ***
**********************************************************************

cap file close tables 
file open tables using "../output/table5b_predictsmall.txt", write replace
file write tables "<tab:predictsmallb>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in evergrad everpsat psatscoreoverall zcstmath11 /*zcstela11*/ zcstscience8 zcstscience10 ///
	zcstsocial8 /*zcstsocial11*/ zcstsocialworld apclasstotal { 
	reg `var' cog_factor jackson_factor noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode) 
	estimate store model`counter'	
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table5b_predictsmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(cog_factor jackson_factor noncog_factor)
	
	
******************************************************
*** TABLE 6: Causal VA results using all variables ***
******************************************************
cap file close tables 
file open tables using "../output/table6_vabig.txt", write replace
file write tables "<tab:vabig>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_habmarktotalyr ztva_effmarktotalyr /// 
			ztva_suspended ztva_logdaysabsent ztva_heldback  `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table6_vabig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_habmarktotalyr ztva_effmarktotalyr ztva_suspended ztva_logdaysabsent ztva_heldback)

**********************************************************************
*** TABLE 6a: Replication of Causal VA results using all variables ***
**********************************************************************
cap file close tables 
file open tables using "../output/table6a_vabig.txt", write replace
file write tables "<tab:vabiga>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' r_ztva_zcstmath r_ztva_zcstela r_ztva_gpatotal r_ztva_effmarktotalyr r_ztva_habmarkfirst5totalyr r_ztva_habmarklast9totalyr /// 
			r_ztva_suspended r_ztva_logdaysabsent r_ztva_heldback  `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table6a_vabig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(r_ztva_zcstmath r_ztva_zcstela r_ztva_gpatotal r_ztva_effmarktotalyr r_ztva_habmarkfirst5totalyr r_ztva_habmarklast9totalyr r_ztva_suspended r_ztva_logdaysabsent r_ztva_heldback)

	
**********************************************************
*** TABLE 7: Causal estimates using 3 factor variables ***
**********************************************************

cap file close tables 
file open tables using "../output/table7_vasmall.txt", write replace
file write tables "<tab:vasmall>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)

********************************************************************************
*** TABLE 7b: Causal estimates w/3 factors and contemperanous Jackson Factor ***
********************************************************************************

cap file close tables 
file open tables using "../output/table7b_vasmall.txt", write replace
file write tables "<tab:vasmallb>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor_contemp ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7b_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor_contemp ztva_noncog_factor)
	
*************************************************************************
*** TABLE 7c: Causal estimates using noncog factor estimate using EP1 ***
*************************************************************************

cap file close tables 
file open tables using "../output/table7c_vasmall.txt", write replace
file write tables "<tab:vasmallc>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncogep1_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7c_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncogep1_factor)
	
**********************************************************************
*** TABLE 7d: Causal estimates using combined Jackson-NonCog Factor***
**********************************************************************

cap file close tables 
file open tables using "../output/table7d_vasmall.txt", write replace
file write tables "<tab:vasmalld>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_allnoncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7d_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_allnoncog_factor)

**********************************************************
*** TABLE 7e: Causal estimates using 2 agnostic factors***
**********************************************************

cap file close tables 
file open tables using "../output/table7e_vasmall.txt", write replace
file write tables "<tab:vasmalle>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_agnostic2_factor ztva_agnostic1_factor  ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7e_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_agnostic1_factor ztva_agnostic2_factor)
	

********************************************************************************
*** TABLE 7f: Causal estimates w/3 factors and contemperanous Noncog Factor ***
********************************************************************************

cap file close tables 
file open tables using "../output/table7f_vasmall.txt", write replace
file write tables "<tab:vasmallf>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor_contemp ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7f_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor_contemp)

**************************************************************************************
*** TABLE 7g: Causal estimates w/3 factors computed first then TVA computed second ***
**************************************************************************************

cap file close tables 
file open tables using "../output/table7g_vasmall.txt", write replace
file write tables "<tab:vasmallg>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_post ztva_jackson_factor_post ztva_noncog_factor_post ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7g_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_post ztva_jackson_factor_post ztva_noncog_factor_post)

*********************************************************
*** TABLE 7h: Causal estimates w/3 replicated factors ***
*********************************************************

cap file close tables 
file open tables using "../output/table7h_vasmall.txt", write replace
file write tables "<tab:vasmallh>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_rep ztva_jackson_factor_rep ztva_noncog_factor_rep ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7h_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_rep ztva_jackson_factor_rep ztva_noncog_factor_rep)

**************************************************************
*** TABLE 7j: Causal estimates by grade ***
**************************************************************	
cap file close tables 
file open tables using "../output/table7j_vasmall.txt", write replace
file write tables "<tab:vasmallj>" _n
cap file close tables 


forvalues grade=3/5{
local counter = 1
local modellist`grade' = ""
	foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
			`ctrls'	if grade==`grade' , cluster(hs_cdscode)
		estimate store model`counter'
		local modellist`grade' "`modellist`grade'' model`counter'"
		local ++counter
	
	}
	estout `modellist`grade'' using ../output/table7j_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
		stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)
}

**************************************************************
*** TABLE 7k: Causal estimates jackson - all gpas ***
**************************************************************

cap file close tables 
file open tables using "../output/table7k_vasmall.txt", write replace
file write tables "<tab:vasmallk>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor_all1 ztva_jackson_factor_all2 ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7k_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor_all1 ztva_jackson_factor_all2 ztva_noncog_factor)

**************************************************************
*** TABLE 7l: Causal estimates nocog - all habits ***
**************************************************************

cap file close tables 
file open tables using "../output/table7l_vasmall.txt", write replace
file write tables "<tab:vasmalll>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor_all1 ztva_noncog_factor_all2 ztva_noncog_factor_all3 ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7l_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor_all1 ztva_noncog_factor_all2 ztva_noncog_factor_all3)

	
************************************************************************
*** TABLE 7m: Causal estimates agnostic factors - all habits and gpa ***
************************************************************************
cap file close tables 
file open tables using "../output/table7m_vasmall.txt", write replace
file write tables "<tab:vasmallm>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_agnostic1_factor_all ztva_agnostic2_factor_all ztva_agnostic3_factor_all ztva_agnostic4_factor_all ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7m_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_agnostic1_factor_all ztva_agnostic2_factor_all ztva_agnostic3_factor_all ztva_agnostic4_factor_all)
	
************************************************************************
*** TABLE 7n: Causal estimates agnostic factors - leaveout cog factor ***
************************************************************************
cap file close tables 
file open tables using "../output/table7n_vasmall.txt", write replace
file write tables "<tab:vasmalln>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_leaveout ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7n_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_leaveout ztva_jackson_factor ztva_noncog_factor)
	
************************************************************************
*** TABLE 7o: Causal estimates agnostic factors - only cog and noncog ***
************************************************************************	
cap file close tables 
file open tables using "../output/table7o_vasmall.txt", write replace
file write tables "<tab:vasmallo>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7o_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_noncog_factor)	

************************************************************************
*** TABLE 7p: Causal estimates agnostic factors - only cog and noncog ***
************************************************************************	
cap file close tables 
file open tables using "../output/table7p_vasmall.txt", write replace
file write tables "<tab:vasmallp>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_inschool_factor ztva_allgpas_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7p_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_inschool_factor ztva_allgpas_factor )	

************************************************************************
*** TABLE 7q: Causal estimates using other outcomes ***
************************************************************************	
cap file close tables 
file open tables using "../output/table7q_vasmall.txt", write replace
file write tables "<tab:vasmallq>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in evergrad everpsat psatscoreoverall zcstmath11 zcstela11 zcstscience8 zcstscience10 ///
	zcstsocial8 zcstsocial11 zcstsocialworld apclasstotal 	 { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7q_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)	
	
**********************************************************
*** TABLE 7r: Causal estimates using 3 factor variables ***
**********************************************************

cap file close tables 
file open tables using "../output/table7r_vasmall.txt", write replace
file write tables "<tab:vasmallr>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_orig ztva_jackson_factor_orig ztva_noncog_factor_orig ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7r_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_orig ztva_jackson_factor_orig ztva_noncog_factor_orig)
 

**************************************************************************
*** TABLE 7s: Causal estimates using 3 factor variables - Teacher FE VA***
**************************************************************************

cap file close tables 
file open tables using "../output/table7s_vasmall.txt", write replace
file write tables "<tab:vasmalls>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_chetty ztva_jackson_factor_chetty ztva_noncog_factor_chetty ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7s_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_chetty ztva_jackson_factor_chetty ztva_noncog_factor_chetty)

**************************************************************************
*** TABLE 7t: Causal estimates using 3 factor variables - Chetty Rep.  ***
**************************************************************************

cap file close tables 
file open tables using "../output/table7t_vasmall.txt", write replace
file write tables "<tab:vasmallt>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	areg `var' `ctrls' if grade >= 3 & grade <= 5 & ztva_cog_factor_chetty!=. & ztva_jackson_factor_chetty !=. /// 
		& ztva_noncog_factor_chetty !=., absorb(teacherid)
	predict res_`var' if grade >= 3 & grade <= 5 & ztva_cog_factor_chetty!=. & ztva_jackson_factor_chetty !=. /// 
		& ztva_noncog_factor_chetty !=., dr
	
	reg res_`var' ztva_cog_factor_chetty ztva_jackson_factor_chetty ztva_noncog_factor_chetty  ///
		if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7t_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_chetty ztva_jackson_factor_chetty ztva_noncog_factor_chetty)

**********************************************************
*** TABLE 7u: Causal estimates using 3 factor variables ***
**********************************************************

cap file close tables 
file open tables using "../output/table7u_vasmall.txt", write replace
file write tables "<tab:vasmallu>" _n
cap file close tables 

foreach var in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
	satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela{ 
		egen `var'_pctile=pc(`var'), by(grade year)
		if inlist("`var'","ztva_cog_factor","ztva_jackson_factor","ztva_noncog_factor")==1{
			replace `var'_pctile = `var'_pctile/10
		}
}
local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal_pctile hs_gpatotal_pctile hs_workeffortgpatotal_pctile hs_coopgpatotal_pctile  ///
	zcahseescoremath_pctile zcahseescoreela_pctile hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_pctile ztva_jackson_factor_pctile ztva_noncog_factor_pctile ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7u_vasmall.txt, style(tab) append cells(b(fmt(%6.4f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_pctile ztva_jackson_factor_pctile ztva_noncog_factor_pctile)
drop *_pctile	

**********************************************************
*** TABLE 7w: Causal estimates using 3 factor variables ***
**********************************************************
cap file close tables 
file open tables using "../output/table7w_vasmall.txt", write replace
file write tables "<tab:vasmallw>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7w_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)
	

*******************************************************************************************************************
*** TABLE 7x: Causal estimates using 3 factor variables estimated using leads and lags of teacher fixed effects ***
*******************************************************************************************************************

cap file close tables 
file open tables using "../output/table7x_vasmall.txt", write replace
file write tables "<tab:vasmall>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor_wfe ztva_noncog_factor_wfe ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7x_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor_wfe ztva_noncog_factor_wfe)
	


**********************************************************
*** TABLE 7 Heterogeneity Test: Causal estimates using 3 factor variables for above and below the median test score ***
**********************************************************

gen lagzcstmath = L.zcstmath 
gen lagzcstela = L.zcstela 
gen lagzcstboth = (lagzcstmath + lagzcstela)/2

sum lagzcstboth if grade >= 3 & grade <= 5, detail
local medlagtestscores = r(p50)
di `medlagtestscores'

cap file close tables 
file open tables using "../output/table7hetero_vasmall.txt", write replace
file write tables "<tab:vasmall_hetero>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5 & lagzcstboth < `medlagtestscores', cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7hetero_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)


local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5 & lagzcstboth >= `medlagtestscores' & lagzcstboth < ., cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7hetero_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)

drop lagzcstmath lagzcstela lagzcstboth

*******************************************************************************************************************
*** TABLE 9: 1st Stage ***
*******************************************************************************************************************

cap file close tables 
file open tables using "../output/table9_1st_stage.txt", write replace
file write tables "<tab:1st_stage>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in cog_factor jackson_factor noncog_factor{ 
	reg F.`var' ztva_`var' ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

foreach var in cog_factor jackson_factor noncog_factor{ 
	reg F.`var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table9_1st_stage.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_*_factor)


local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg F.`var' ztva_cog_factor ztva_jackson_factor  ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}


******************************************************
*** TABLE 9b: 1st Stage using all variables ***
******************************************************
cap file close tables 
file open tables using "../output/table9b_1st_stagebig.txt", write replace
file write tables "<tab:1st_stagebig>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in  zcstmath zcstela gpatotal habmarktotalyr effmarktotalyr /// 
			suspended logdaysabsent heldback{ 
	reg F.`var' ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_habmarktotalyr ztva_effmarktotalyr /// 
			ztva_suspended ztva_logdaysabsent ztva_heldback  `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table9b_1st_stagebig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_habmarktotalyr ztva_effmarktotalyr ztva_suspended ztva_logdaysabsent ztva_heldback)


*******************************************************************************************************************
*** TABLE 10: Second Stage Stage ***
*******************************************************************************************************************
preserve
drop j_* oth_* c_* r_* zcstsocial8 zcstsocial11 zcstsocialworld gpaart-apsemesters zcstscience5- habmarktotalep3 habmarkauthoritytotalyr- habmarkuseoftimetotalyr ///
	evergrad gpalisten gparead gpaspeak gpawrite leavelausd apclasstotal hs_z*
drop gpascience2-gpaother3 tv* ztva_habmarkauthoritytotalyr-ztva_habmarkuseoftimetotalyr ztva_gpascience-ztva_gpaart ztva_cog_factor_orig-ztva_agnostic4_factor_all	///
	ztva_inschool_factor-noncog_factor_orig ztva_cog_factor_post-ztva2l_noncog_factor_post
drop stdpseudoid tchpseudoidfrommarks psatscoreoverall teacherid dayssuspended numofsuspendions ///
	achmarktotalyr habmarktotalyr habmarkfirst5totalep1 habmarkfirst5totalep3 habmarkfirst5totalyr ///
	habmarklast9totalep1 habmarklast9totalep3 habmarklast9totalyr classid everpsat fracdaysabsent /// 
	hs_fracdaysabsent hs_suspended achmarkenglishtotalyr hs_leavelausd zgpatotal zeffmarktotalyr /// 
	zhabmarktotalyr attrition
drop grade_Lhabmarkfirst5totalyr class_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr /// 
	class_Lhabmarklast9totalyr grade_Lhabmarkfirst5totalep3 class_Lhabmarkfirst5totalep3 /// 
	grade_Lhabmarklast9totalep3 class_Lhabmarklast9totalep3 gpatotal12 workeffortgpatotal12 /// 
	coopgpatotal12 dayssuspended12 logdaysabsent12 zcstmath11 zcstela11 heldback11 /// 
	habmarkfirst5totalyr2 grade_Lhabmarkfirst5totalyr2 class_Lhabmarkfirst5totalyr2 /// 
	habmarkfirst5totalyr3 grade_Lhabmarkfirst5totalyr3 class_Lhabmarkfirst5totalyr3 /// 
	habmarklast9totalyr2 grade_Lhabmarklast9totalyr2 class_Lhabmarklast9totalyr2 /// 
	habmarklast9totalyr3 grade_Lhabmarklast9totalyr3 class_Lhabmarklast9totalyr3 ///
	habmarkfirst5totalep32-class_Lhabmarklast9totalep33 gpaenglish? gpaenglish? ///
	ztva_zcstmath-ztva_gpamath 
	
cap file close tables 
file open tables using "../output/table10_2sls.txt", write replace
file write tables "<tab:2sls_stage>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach facvar in cog_factor jackson_factor noncog_factor{ 
	foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
		zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		gen F`facvar'=F.`facvar'
		ivreg2 `var' `ctrls' (F`facvar' = ztva_`facvar') /// 
			if grade >= 3 & grade <= 5, partial(`ctrls_split2') cluster(hs_cdscode) first
		estimate store model`counter'
		local modellist "`modellist' model`counter'"
		local ++counter
		drop F`facvar'
	}
	estout `modellist' using ../output/table10_2sls.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(*_factor)

	local counter = 1
	local modellist = ""
}
restore

	
**********************************************************************
*** TABLE 17: Habits Breakdown ***
**********************************************************************
cap file close tables 
file open tables using "../output/table17_habitsvabig.txt", write replace
file write tables "<tab:habitsbreakdown>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_habmarkuseoftimetotalyr ztva_habmarkindependenttotalyr ztva_habmarkorganizedtotalyr ztva_habmarkcarefultotalyr  ///
		 ztva_habmarkontimetotalyr ztva_habmarkfollowstotalyr ztva_habmarkauthoritytotalyr ztva_habmarkcooperatestotalyr ///
		 ztva_habmarkdependabletotalyr ztva_habmarkresponsibletotalyr ztva_habmarkselfcontroltotalyr ztva_habmarkresolvestotalyr ///
		 ztva_habmarksocialtotalyr ztva_habmarkfairplaytotalyr `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table17_habitsvabig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_habmarkuseoftimetotalyr ztva_habmarkindependenttotalyr ztva_habmarkorganizedtotalyr ztva_habmarkcarefultotalyr ztva_habmarkontimetotalyr ztva_habmarkfollowstotalyr ztva_habmarkauthoritytotalyr ztva_habmarkcooperatestotalyr ztva_habmarkdependabletotalyr ztva_habmarkresponsibletotalyr ztva_habmarkselfcontroltotalyr ztva_habmarkresolvestotalyr ztva_habmarksocialtotalyr ztva_habmarkfairplaytotalyr)

	
**********************************************************************
*** TABLE 18: Grades Breakdown ***
**********************************************************************
cap file close tables 
file open tables using "../output/table18_gradesvabig.txt", write replace
file write tables "<tab:gradesbreakdown>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ///
				ztva_gpahealth ztva_gpape ztva_gpaart `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table18_gradesvabig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart )
	
	
**********************************************************************
*** TABLE 18: Grades Breakdown ***
**********************************************************************
cap file close tables 
file open tables using "../output/table18_predictedgradesbig.txt", write replace
file write tables "<tab:predictedgradesbreakdown>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience ///
				gpahealth gpape gpaart `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table18_predictedgradesbig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart)
	
	
**********************************************************************
*** TABLE 18b: Grades Breakdown - with Individual Grade Controls  ***
**********************************************************************
cap file close tables 
file open tables using "../output/table18b_gradesvabig.txt", write replace
file write tables "<tab:gradesbreakdownb>" _n
cap file close tables 

#delimit ;
local additional_grade_ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpamath L.gpascience L.gpasocial L.gpalisten L.gparead L.gpaspeak 
	L.gpawrite L.gpahealth L.gpape L.gpaart L.gpamath? L.gpascience? L.gpasocial? L.gpalisten? L.gparead? L.gpaspeak? L.gpawrite? 
	L.gpahealth?  L.gpape? L.gpaart?)";
#delimit cr
	
local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ///
				ztva_gpahealth ztva_gpape ztva_gpaart `ctrls' `additional_grade_ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table18b_gradesvabig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart )
	
	
**********************************************************************
*** TABLE 18b: Grades Breakdown - with Individual Grade Controls ***
**********************************************************************
cap file close tables 
file open tables using "../output/table18b_predictedgradesbig.txt", write replace
file write tables "<tab:predictedgradesbreakdownb>" _n
cap file close tables 

#delimit ;
local additional_grade_ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpamath L.gpascience L.gpasocial L.gpalisten L.gparead L.gpaspeak 
	L.gpawrite L.gpahealth L.gpape L.gpaart L.gpamath? L.gpascience? L.gpasocial? L.gpalisten? L.gparead? L.gpaspeak? L.gpawrite? 
	L.gpahealth?  L.gpape? L.gpaart?)";
#delimit cr

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience ///
				gpahealth gpape gpaart `ctrls' `additional_grade_ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table18b_predictedgradesbig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart)

**********************************************************************
*** TABLE 18: Grades Breakdown - High School***
**********************************************************************
#delimit ;

local controls_secondary "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.zcstmath? 
	L.zcstela? L.gpatotal? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
#delimit cr	


cap file close tables 
file open tables using "../output/table18_gradesvabighs.txt", write replace
file write tables "<tab:gradesbreakdownhs>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_gpamath ztva_gpaenglish ztva_gpasocial ztva_gpascience `controls_secondary' ///
			if grade >= 6 & grade <= 12, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table18_gradesvabighs.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_gpamath ztva_gpaenglish ztva_gpasocial ztva_gpascience )
	
	
**********************************************************
*** TABLE 19: Effect on predicted outcomes; selection test ***
**********************************************************
#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local tchtotal_l1controls "i.grade#C.(L2.zcstmath L2.zcstela L2.gpatotal L2.effmarktotalyr L2.zcstmath? 
	L2.zcstela? L2.gpatotal? L2.effmarktotalyr? L2.logdaysabsent L2.suspended L2.heldback) 
	L.ell i.L.grade i.L.year";
	
#delimit cr

cap file close tables 
file open tables using "../output/table8_predicted.txt", write replace
file write tables "<tab:predicted>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' `tchtotal_l1controls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	predict resid_`var' if e(sample) == 1, xb
	
	reg resid_`var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' ///
		if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'	
	local modellist "`modellist' model`counter'"
	local ++counter
	
	drop resid_`var'
}

estout `modellist' using ../output/table8_predicted.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)

	
**********************************************************
*** TABLE 19b: Effect on predicted outcomes; selection test for other outcomes ***
**********************************************************
#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local tchtotal_l1controls "i.grade#C.(L2.zcstmath L2.zcstela L2.gpatotal L2.effmarktotalyr L2.zcstmath? 
	L2.zcstela? L2.gpatotal? L2.effmarktotalyr? L2.logdaysabsent L2.suspended L2.heldback) 
	L.ell i.L.grade i.L.year";
	
#delimit cr
		
cap file close tables 
file open tables using "../output/table8b_predicted_otheroutcomes.txt", write replace
file write tables "<tab:predicted_b>" _n
cap file close tables 

local counter = 1
local modellist = ""

reg evergrad `tchtotal_l1controls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	predict resid_evergrad if e(sample) == 1, xb

reg resid_evergrad ztva_cog_factor ztva_jackson_factor ztva_noncog_factor i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? ///
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback) ///
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr ///
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr?  ///
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell ///
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr ///
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? /// 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) /// 
	ell i.year ///
	if grade >= 3 & grade <= 5, cluster(hs_cdscode)

estimate store model`counter'	
local modellist "`modellist' model`counter'"
local ++counter

drop resid_evergrad

foreach var in everpsat psatscoreoverall zcstmath11 zcstscience8 zcstscience10 ///
	zcstsocial8 zcstsocial11 zcstsocialworld apclasstotal { 
	reg `var' `tchtotal_l1controls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	predict resid_`var' if e(sample) == 1, xb
	
	reg resid_`var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' ///
		if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'	
	local modellist "`modellist' model`counter'"
	local ++counter
	
	drop resid_`var'
}

estout `modellist' using ../output/table8b_predicted_otheroutcomes.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)

	
**********************************************************************
*** TABLE 20g: Testing Bias using score_r from vam - Student Level ***
**********************************************************************
#delimit ;

global controls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.habmarkfirst5totalyr L.habmarklast9totalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.habmarkfirst5totalyr? L.habmarklast9totalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

cap file close tables 
file open tables using "../output/table20g_va_class.txt", write replace
file write tables "<tab:vatest_classg>" _n

use ../data/lausd_data/vascores/biastest_studentlevel, clear 
merge 1:1 year studentid using ../data/lausd_data/vascores/twoyrlag_residvariables, nogen

gen kinderyear = year - grade  
egen cohortsch = group(cdscode kinderyear)

xtset studentid year

*** Residualize all of the variables in terms of the basline controls 
foreach var in zcstmath zcstela habmarktotalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	reg score_r_`var' $controls 
	predict resid_`var', residual
	
	reg L2.`var' $controls 
	predict resid_2lag`var', residual
}

*** Predict outcomes
foreach var in zcstmath zcstela habmarktotalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	reg resid_`var' resid_2lagzcstmath resid_2lagzcstela resid_2laghabmarktotalyr resid_2laggpatotal ///
		resid_2lageffmarktotalyr resid_2laglogdaysabsent resid_2lagsuspended resid_2lagheldback
	predict pred_`var', xb
}

*** Row 1: Test scores on VA score (test coefficients against 1) 
gen temptv = . 
local counter = 1
matrix SE = J(1,8,.)
matrix OBS = J(1,8,.)
foreach var in zcstmath zcstela habmarktotalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	replace temptv = . 
	replace temptv = tv_`var'
	reg score_r_`var' temptv, cluster(hs_cdscode)
	global pvalue = 2*ttail(e(N) - e(df_m),abs((_b[temptv] - 1)/_se[temptv]))
	file write tables %9.3f (_b[temptv]) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
	matrix SE[1,`counter'] = _se[temptv]
	matrix OBS[1,`counter'] = e(N)		
	local ++counter
}
drop temptv 

file write tables _n 
forvalues x = 1/8 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 	

forvalues x = 1/8 {
	file write tables (OBS[1,`x']) _tab 
}
file write tables _n 



*** Row 2: Estimate forcast bias with predicted outcomes (test coefficients against 0) 
gen temptvpred = .
local counter = 1
matrix SE = J(1,8,.)
matrix OBS = J(1,8,.)
foreach var in zcstmath zcstela habmarktotalyr gpatotal effmarktotalyr logdaysabsent suspended heldback {
	replace temptvpred = . 
	replace temptvpred = tv_`var'
	reg pred_`var' temptvpred, cluster(hs_cdscode)
	global pvalue = 2*ttail(e(N) - e(df_m),abs((_b[temptvpred])/_se[temptvpred]))
	file write tables %9.3f (_b[temptvpred]) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
	matrix SE[1,`counter'] = _se[temptvpred]
	matrix OBS[1,`counter'] = e(N)			
	local ++counter
}
drop temptvpred

file write tables _n 
forvalues x = 1/8 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 	

forvalues x = 1/8 {
	file write tables (OBS[1,`x']) _tab 
}
file write tables _n 


cap file close tables 

**** Binscatter plots of forecast bias

label var zcstmath "Math Test Scores"
label var zcstela "English Test Scores"
label var habmarktotalyr "Habit GPA"
label var gpatotal "GPA"
label var effmarktotalyr "Effort GPA"
label var logdaysabsent "Log Absences"
label var heldback "Heldback a Grade"
label var suspended "Suspended"

local ymin_zcstmath = .6
local ytick_zcstmath = .2

local ymin_zcstela = .4
local ytick_zcstela = .2

local ymin_habmarktotalyr = .2
local ytick_habmarktotalyr = .1

local ymin_gpatotal = .3
local ytick_gpatotal = .1

local ymin_effmarktotalyr = .3
local ytick_effmarktotalyr = .1

local ymin_logdaysabsent = .15
local ytick_logdaysabsent = .05

local ymin_suspended = .015
local ytick_suspended = .005

local ymin_heldback = .004
local ytick_heldback = .002

** Actual 
foreach depvar in zcstmath zcstela habmarktotalyr gpatotal effmarktotalyr logdaysabsent suspended heldback { 
   	reg score_r_`depvar' tv_`depvar', cluster(hs_cdscode)
   	local tempcoef: display %5.3fc _b[tv_`depvar']
   	di `tempcoef'
   	local tempse: display %5.3fc _se[tv_`depvar'] 
   	di `tempse'
	binscatter score_r_`depvar' tv_`depvar', mcolors(black) lcolors(black) ///
   		ytitle(`: variable label `depvar'', color(black)) xtitle("Teacher Value-Added", color(black)) ///
   		title(`: variable label `depvar'', color(black)) ///
   		ylabel(-`ymin_`depvar''(`ytick_`depvar'')`ymin_`depvar'', nogrid) xlabel(-`ymin_`depvar''(`ytick_`depvar'')`ymin_`depvar'') ///
    	note("Coef = `tempcoef' " "(`tempse')", box fcolor(none) placement(5) ring(0) justification(right) size(large)) ///
    	savegraph(../output/forcastbias_binscat_`depvar'.png) replace 

}	   	
	
** Predicted
foreach depvar in zcstmath zcstela habmarktotalyr gpatotal effmarktotalyr logdaysabsent suspended heldback { 
   	reg pred_`depvar' tv_`depvar', cluster(hs_cdscode)
   	local tempcoef: display %5.3fc _b[tv_`depvar']
   	di `tempcoef'
   	local tempse: display %5.3fc _se[tv_`depvar'] 
   	di `tempse'
	binscatter pred_`depvar' tv_`depvar', mcolors(black) lcolors(black) ///
   		ytitle(Predicted `: variable label `depvar'', color(black)) xtitle("Teacher Value-Added", color(black)) ///
   		title(Predicted `: variable label `depvar'', color(black)) ///
    	ylabel(-`ymin_`depvar''(`ytick_`depvar'')`ymin_`depvar'', nogrid) xlabel(-`ymin_`depvar''(`ytick_`depvar'')`ymin_`depvar'') ///
    	note("Coef = `tempcoef' " "(`tempse')", box fcolor(none) placement(5) ring(0) justification(right) size(large)) ///
    	savegraph(../output/forcastbiaspredict_binscat_`depvar'.png) replace 

}	   	
	
	
**********************************************************************
*** TABLE 20h: Testing Bias using score_r from vam - School-Grade Level ***
**********************************************************************
*********** Create School-Grade Forecast Bias Table
cap file close tables 
file open tables using "../output/table20h_va_schgrade.txt", write replace
file write tables "<tab:vatest_schgrade_h>" _n

local counter = 1
matrix SE = J(1,8,.)
matrix OBS = J(1,8,.)
foreach var in zcstmath zcstela gpatotal habmarktotalyr effmarktotalyr logdaysabsent suspended heldback {
	use "../data/lausd_data/temp/tempdata_school_bias_test.dta", clear
	xtset studentid year

	egen school_grade = group(cdscode grade)
	keep if L.`var' != . & `var' != . & tv_2yr_l_`var' != . & tv_2yr_f_`var' != . 

	collapse (mean) `var' tchyrcount score_r_`var' tv_2yr_l_`var' tv_`var' tv_2yr_f_`var' tv_ss_`var' ///
		(median) cdscode cohortsch, by(school_grade year)

	xtset school_grade year

	gen diff_`var' = `var' - L.`var'
	gen diff_score_r_`var' = score_r_`var' - L.score_r_`var'

	gen diff_va_`var' = tv_2yr_l_`var' - L.tv_2yr_f_`var'
	gen diff_va_`var'2 = tv_ss_`var' - L.tv_ss_`var'

	egen schyr = group(cdscode year)
	
	gen tempvar = diff_va_`var'
	
	reg diff_`var' tempvar, cluster(school_grade)
	global pvalue = 2*ttail(e(N) - e(df_m),abs((_b[tempvar] - 1)/_se[tempvar]))
	file write tables %9.3f (_b[tempvar]) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
	matrix SE[1,`counter'] = _se[tempvar]
	matrix OBS[1,`counter'] = e(N)
	local ++counter
	drop tempvar

}

file write tables _n 
forvalues x = 1/8 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 	

forvalues x = 1/8 {
	file write tables (OBS[1,`x']) _tab 
}
file write tables _n 

cap file close tables

*********************************************************************************************
*** TABLE 21a: Causal estimates using 3 factor variables - Class Level - Chetty Approach ***
*********************************************************************************************
local counter = 1
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback {
	use studentid teacherid year cdscode grade `var' ztva2l_cog_factor_post ztva2f_cog_factor_post ztva2f_jackson_factor_post  ztva2l_jackson_factor_post ///
		ztva2f_noncog_factor_post ztva2l_noncog_factor_post cohortsch hs_cdscode ///
		using  ../data/lausd_data/analysis/student_year_main if grade>=2 & grade<=5, clear
	
	xtset studentid year
	fvset base 2007 year
	fvset base 3 grade
	
	egen school_grade = group(cdscode grade)

	**Check for teachers with missing value added scores
	foreach factor in cog_factor jackson_factor	noncog_factor{
	
		* NonMissing TVA by Teacher-Year?
		egen tnonmissingf_`factor' =  total(ztva2f_`factor'_post!=.), by(teacherid year)
		egen tnonmissingl_`factor' =  total(ztva2l_`factor'_post!=.), by(teacherid year)
		
		* All Teachers Nonmissing TVA by Grade-Year
		egen gnonmissingf_`factor' =  min(tnonmissingf_`factor'), by(school_grade year)
		egen gnonmissingl_`factor' =  min(tnonmissingl_`factor'), by(school_grade year)
	}
	egen missingteacher = rowtotal(gnonmissingf_* gnonmissingl_*) 
	keep if missingteacher==0
		
	keep if L.`var' != . & `var' != . & ztva2l_cog_factor_post!= . & ztva2f_cog_factor_post!= . & /// 
		ztva2f_jackson_factor_post!= . &  ztva2l_jackson_factor_post!= . & ztva2f_noncog_factor_post!= . & /// 
		ztva2l_noncog_factor_post
		
	collapse (mean) `var' ztva2l_cog_factor_post ztva2f_cog_factor_post ztva2f_jackson_factor_post  ///
		ztva2l_jackson_factor_post ztva2f_noncog_factor_post ztva2l_noncog_factor_post ///
		(median) cdscode cohortsch (count) studentid, by(school_grade year)

	xtset school_grade year
	foreach vavar in cog_factor_post jackson_factor_post noncog_factor_post{
		gen diff_va_`vavar' = ztva2l_`vavar' - L.ztva2f_`vavar'
	}
	
	areg D.`var' diff_va_cog_factor_post diff_va_jackson_factor_post diff_va_noncog_factor_post [aw=studentid], cluster(school_grade) absorb(year)
	
	global pvaluecog = 2*ttail(e(df_r),abs((_b[diff_va_cog_factor_post])/_se[diff_va_cog_factor_post]))
	global pvaluejack = 2*ttail(e(df_r),abs((_b[diff_va_jackson_factor_post])/_se[diff_va_jackson_factor_post]))
	global pvaluenoncog = 2*ttail(e(df_r),abs((_b[diff_va_noncog_factor_post])/_se[diff_va_noncog_factor_post]))
	
	
	clear 
	set obs 8
	gen row=_n
	gen var`counter' = ""
	replace var`counter' = string(round(_b[diff_va_cog_factor_post],.001),"%9.3f")+(cond($pvaluecog<.01,"***",cond($pvaluecog<.05,"**",cond($pvaluecog<.1,"*","")))) if _n==1
	replace var`counter' = string(round(_se[diff_va_cog_factor_post],.001),"%9.3f")  if _n==2
	replace var`counter' = string(round(_b[diff_va_jackson_factor_post],.001),"%9.3f")+(cond($pvaluejack<.01,"***",cond($pvaluejack<.05,"**",cond($pvaluejack<.1,"*","")))) if _n==3
	replace var`counter' = string(round(_se[diff_va_jackson_factor_post],.001),"%9.3f")  if _n==4
	replace var`counter' = string(round(_b[diff_va_noncog_factor_post],.001),"%9.3f")+(cond($pvaluenoncog<.01,"***",cond($pvaluenoncog<.05,"**",cond($pvaluenoncog<.1,"*","")))) if _n==5
	replace var`counter' = string(round(_se[diff_va_noncog_factor_post],.001),"%9.3f")  if _n==6
	replace var`counter' = string(e(N),"%9.0f") if _n==7
	replace var`counter' = string(round(e(r2),.001),"%9.3f") if _n==8
	saveold ../data/lausd_data/temp/var`counter', replace
	local ++counter
}
use 	../data/lausd_data/temp/var1, clear
forvalues counter=2/11{
	merge 1:1 row using ../data/lausd_data/temp/var`counter', nogen
}

cap file close tables 
file open tables using "../output/table21a_va_class.txt", write replace
file write tables "<tab:va_classa>" _n
forvalues row=1/8{
	forvalues column=1/11{
		if `column'<11{
			file write tables (var`column'[`row']) _tab
		}
		else{
			file write tables (var`column'[`row']) _n
		}
	}
}
cap file close tables 


**********************************************************************
*** TABLE 22a: Effects by Grade - Secondary ***
**********************************************************************
#delimit ;

local controls_secondary "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.zcstmath? 
	L.zcstela? L.gpatotal? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
#delimit cr	


cap file close tables 
file open tables using "../output/table22a_vasmall.txt", write replace
file write tables "<tab:vasmall_secondary>" _n
cap file close tables 

use  ../data/lausd_data/analysis/student_year_main , clear
xtset studentid year

forvalues grade=6/12{
local counter = 1
local modellist`grade' = ""
	foreach var in evergrad eversat satscoretotal  gpatotal12 workeffortgpatotal12 coopgpatotal12 ///
		zcstmath11 zcstela11 dayssuspended12 logdaysabsent12 heldback11 { 
		reg `var' ztva_cog_factor ztva_jackson_factor `controls_secondary' ///
				if grade == `grade', cluster(hs_cdscode)
		estimate store model`counter'
		local modellist`grade' "`modellist`grade'' model`counter'"
		local ++counter
	}

	estout `modellist`grade'' using ../output/table22a_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
		stats(, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
		keep(ztva_cog_factor ztva_jackson_factor)
}

**********************************************************************
*** TABLE 22b: Effects by Grade - Secondary - Contemporaneous Factor***
**********************************************************************
cap file close tables 
file open tables using "../output/table22b_vasmall.txt", write replace
file write tables "<tab:vasmall_secondary_contemp>" _n
cap file close tables 

forvalues grade=6/12{
local counter = 1
local modellist`grade' = ""
	foreach var in evergrad eversat satscoretotal  gpatotal12 workeffortgpatotal12 coopgpatotal12 ///
		zcstmath11 zcstela11 dayssuspended12 logdaysabsent12 heldback11 { 
		reg `var' ztva_cog_factor ztva_jackson_factor_contemp `controls_secondary' ///
				if grade == `grade', cluster(hs_cdscode)
		estimate store model`counter'
		local modellist`grade' "`modellist`grade'' model`counter'"
		local ++counter
	}

	estout `modellist`grade'' using ../output/table22b_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
		stats(, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
		keep(ztva_cog_factor ztva_jackson_factor_contemp)
}

***************************************************
**** Table 23a: Deselection on True Teacher VA ****
***************************************************
#delimit ;
local standard_va_vars "zcstmath zcstela";

local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

*** Create matrix with coeffificients of long run effects with all three factors (see table 7 in the code)
preserve 

use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

matrix COEFALL = J(3,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix COEFALL[1,`counter'] = _b[ztva_cog_factor]
	matrix COEFALL[2,`counter'] = _b[ztva_jackson_factor]
	matrix COEFALL[3,`counter'] = _b[ztva_noncog_factor]
	local ++counter
}

matrix list COEFALL

*** Correlation matrix between Cognitive, Mixed, and Noncognitive Factors
corr ztva_cog_factor ztva_jackson_factor ztva_noncog_factor if grade >= 3 & grade <= 5
matrix M = r(C)

restore

preserve

clear
set seed 123456
set obs 10000000


** Create all possible equally weighted linear combinations
drawnorm cog mixed noncog, corr(M)

gen allthree = (1/3)*cog + (1/3)*mixed + (1/3)*noncog
gen cogandmixed = (1/2)*cog + (1/2)*mixed
gen cogandnoncog = (1/2)*cog + (1/2)*mixed
gen mixedandnoncog = (1/2)*cog + (1/2)*mixed

** Create globals for the 5 percentile for each linear combination
sum allthree, detail
global p5_allthree = r(p5)

sum cogandmixed, detail
global p5_cogandmixed = r(p5)

sum cogandnoncog, detail
global p5_cogandnoncog = r(p5)

sum mixedandnoncog, detail
global p5_mixedandnoncog = r(p5)

foreach var in cog mixed noncog {
	sum `var', detail
	global p5_`var' = r(p5)	
}

*** Create the average number of standard deviations away from the mean that each factor is 
** for the bottom 5% of a given linear combination 
foreach var in cog mixed noncog { 
	sum `var' if allthree < $p5_allthree
	global bot5mean_allthree_`var' = abs(r(mean))
}

foreach var in cog mixed { 
	sum `var' if cogandmixed < $p5_cogandmixed
	global bot5mean_cogandmixed_`var' = abs(r(mean))
}

foreach var in cog noncog { 
	sum `var' if cogandnoncog < $p5_cogandnoncog
	global bot5mean_cogandnoncog_`var' = abs(r(mean))
}

foreach var in mixed noncog { 
	sum `var' if mixedandnoncog < $p5_mixedandnoncog
	global bot5mean_mixedandnoncog_`var' = abs(r(mean))
}

sum cog if cog < $p5_cog
global bot5mean_cog = abs(r(mean))

sum mixed if mixed < $p5_mixed
global bot5mean_mixed = abs(r(mean))

sum noncog if noncog < $p5_noncog
global bot5mean_noncog = abs(r(mean))

**** Create optimal linear combinations for row 5 of the deselection on true VA table (table 23a) 
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		gen opt_`var' = COEFALL[1,`counter']*cog + COEFALL[2,`counter']*mixed + COEFALL[3,`counter']*noncog
		sum opt_`var', detail 
		global opt_temp = r(p5)
		
		sum cog if opt_`var' < $opt_temp 
		global opt_`var'_cog = abs(r(mean))
		
		sum mixed if opt_`var' < $opt_temp 
		global opt_`var'_mixed = abs(r(mean))		
		
		sum noncog if opt_`var' < $opt_temp 
		global opt_`var'_noncog = abs(r(mean))
		
		local ++counter
}

restore 

***************************************************
**** Table 23a: Deselection on True Teacher VA ****
***************************************************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

cap file close tables 
file open tables using "../output/table23a_deselecttrueva.txt", write replace
file write tables "<tab:table23a_deselecttrueva>" _n

*** Row 1: Deselection of bottom 5% on just Cognitive VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*$bot5mean_cog) _tab 
	matrix SE[1,`counter'] = _se[ztva_cog_factor]*$bot5mean_cog
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 2: Deselection of bottom 5% on just Mixed VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_jackson_factor]*$bot5mean_mixed) _tab 
	matrix SE[1,`counter'] = _se[ztva_jackson_factor]*$bot5mean_mixed
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 3: Deselection of bottom 5% on just Noncognitive VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_noncog_factor]*$bot5mean_noncog) _tab 
	matrix SE[1,`counter'] = _se[ztva_noncog_factor]*$bot5mean_noncog
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 


*** Row 4: Deselection of bottom 5% using both Cognitive and Mixed VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*$bot5mean_cogandmixed_cog + _b[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed) _tab 
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*$bot5mean_cogandmixed_cog)^2 + (_se[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed)^2)
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 5: Deselection of bottom 5% using both Cognitive, Mixed, and Noncognitive VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*$bot5mean_allthree_cog + _b[ztva_jackson_factor]*$bot5mean_allthree_mixed ///
		+ _b[ztva_noncog_factor]*$bot5mean_allthree_noncog) _tab 
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*$bot5mean_allthree_cog)^2 + (_se[ztva_jackson_factor]*$bot5mean_allthree_mixed)^2 ///
		+ (_se[ztva_noncog_factor]*$bot5mean_allthree_noncog)^2)
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 6: Deselection of bottom 5% using the optimal linear combitiona of Cognitive, Mixed, and Noncognitive VA for the given variable
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*${opt_`var'_cog} + _b[ztva_jackson_factor]*${opt_`var'_mixed} ///
		+ _b[ztva_noncog_factor]*${opt_`var'_noncog}) _tab 
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*${opt_`var'_cog})^2 + (_se[ztva_jackson_factor]*${opt_`var'_mixed})^2 ///
		+ (_se[ztva_noncog_factor]*${opt_`var'_noncog})^2)
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

cap file close tables 



********************************************************
**** Table 23b: Deselection on Estimated Teacher VA ****
********************************************************
#delimit ;
local standard_va_vars "zcstmath zcstela";

local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

*** Create matrix with coeffificients of long run effects with all three factors (see table 7 in the code) 
preserve

use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

matrix COEFALL = J(3,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix COEFALL[1,`counter'] = _b[ztva_cog_factor]
	matrix COEFALL[2,`counter'] = _b[ztva_jackson_factor]
	matrix COEFALL[3,`counter'] = _b[ztva_noncog_factor]
	local ++counter
}

matrix list COEFALL



* Create autocorrelation matrix for each VA factor  
use ../data/lausd_data/vascores/biastest_studentlevel, clear

keep if grade >= 2 & grade <= 5 
drop if teacherid == . 

*** Make teacher-year level dataset 
collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr {
		egen fn_`var' = std(score_r_`var')
}

gen score_r_cog_factor = fn_zcstmath + fn_zcstela
gen score_r_jackson_factor_even = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback
gen score_r_noncog_factor_even = fn_effmarktotalyr + fn_habmarkfirst5totalyr + fn_habmarklast9totalyr 

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

*** Create lagged 
foreach var in score_r_cog_factor score_r_jackson_factor score_r_noncog_factor {
*	score_r_zcstmath_math score_r_zcstela_english ///
*	score_r_gpatotal_math score_r_logdaysabsent_math score_r_suspended_math score_r_heldback_math /// 
*	score_r_gpatotal_english score_r_logdaysabsent_english score_r_suspended_english score_r_heldback_english
		gen l1`var' = L1.`var'
		gen l2`var' = L2.`var'
		gen l3`var' = L3.`var'
		gen l4`var' = L4.`var'
		gen l5`var' = L5.`var'
		gen l6`var' = L6.`var'
}

matrix AUTO_all = J(12,12,.)

forvalues x = 1/12 { 
	matrix AUTO_all[`x',`x'] = 1
}

local counter1 = 1 
foreach var1 in score_r_cog_factor l1score_r_cog_factor l2score_r_cog_factor l3score_r_cog_factor score_r_jackson_factor l1score_r_jackson_factor ///
	l2score_r_jackson_factor l3score_r_jackson_factor score_r_noncog_factor l1score_r_noncog_factor l2score_r_noncog_factor l3score_r_noncog_factor { 
		
		local counter2 = 1 
		foreach var2 in score_r_cog_factor l1score_r_cog_factor l2score_r_cog_factor l3score_r_cog_factor score_r_jackson_factor l1score_r_jackson_factor ///
		l2score_r_jackson_factor l3score_r_jackson_factor score_r_noncog_factor l1score_r_noncog_factor l2score_r_noncog_factor l3score_r_noncog_factor { 
			corr `var1' `var2' 
			matrix AUTO_all[`counter1',`counter2'] = r(rho)
			local ++counter2
		}
	local ++counter1
}

forvalues x = 1/12 { 
	matrix AUTO_all[`x',`x'] = 1
}

**** Monte Carlo Simulation to determine expected VA conditional on the estiamted average VA in the previous three is in the bottome 5%. 
clear
set seed 123456
set obs 10000000

drawnorm cog cog1 cog2 cog3 mixed mixed1 mixed2 mixed3 noncog noncog1 noncog2 noncog3, corr(AUTO_all)

gen estcog = (cog1 + cog2 + cog3)/3
gen estmixed = (mixed1 + mixed2 + mixed3)/3
gen estnoncog = (noncog1 + noncog2 + noncog3)/3


** Create all possible equally weighted linear combinations
gen allthree = (1/3)*estcog + (1/3)*estmixed + (1/3)*estnoncog
gen cogandmixed = (1/2)*estcog + (1/2)*estmixed
gen cogandnoncog = (1/2)*estcog + (1/2)*estmixed
gen mixedandnoncog = (1/2)*estcog + (1/2)*estmixed

** Create globals for the 5 percentile for each linear combination
sum allthree, detail
global p5_allthree = r(p5)

sum cogandmixed, detail
global p5_cogandmixed = r(p5)

sum cogandnoncog, detail
global p5_cogandnoncog = r(p5)

sum mixedandnoncog, detail
global p5_mixedandnoncog = r(p5)

foreach var in cog mixed noncog {
	sum est`var', detail
	global p5_`var' = r(p5)	
}

*** Create the average number of standard deviations away from the mean that each factor is 
** for the bottom 5% of a given linear combination 
foreach var in cog mixed noncog { 
	sum `var' if allthree < $p5_allthree
	global bot5mean_allthree_`var' = abs(r(mean))
}

foreach var in cog mixed { 
	sum `var' if cogandmixed < $p5_cogandmixed
	global bot5mean_cogandmixed_`var' = abs(r(mean))
}

foreach var in cog noncog { 
	sum `var' if cogandnoncog < $p5_cogandnoncog
	global bot5mean_cogandnoncog_`var' = abs(r(mean))
}

foreach var in mixed noncog { 
	sum `var' if mixedandnoncog < $p5_mixedandnoncog
	global bot5mean_mixedandnoncog_`var' = abs(r(mean))
}

sum cog if estcog < $p5_cog
global bot5mean_cog = abs(r(mean))

sum mixed if estmixed < $p5_mixed
global bot5mean_mixed = abs(r(mean))

sum noncog if estnoncog < $p5_noncog
global bot5mean_noncog = abs(r(mean))

**** Create optimal linear combinations for row 5 of the deselection on true VA table (table 23a) 
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		gen opt_`var' = COEFALL[1,`counter']*estcog + COEFALL[2,`counter']*estmixed + COEFALL[3,`counter']*estnoncog
		sum opt_`var', detail 
		global opt_temp = r(p5)
		
		sum cog if opt_`var' < $opt_temp 
		global opt_`var'_cog = abs(r(mean))
		
		sum mixed if opt_`var' < $opt_temp 
		global opt_`var'_mixed = abs(r(mean))		
		
		sum noncog if opt_`var' < $opt_temp 
		global opt_`var'_noncog = abs(r(mean))
		
		local ++counter
}
 

*** Create Table
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

cap file close tables 
file open tables using "../output/table23b_deselectestva.txt", write replace
file write tables "<tab:table23b_deselectestva>" _n

*** Row 1: Deselection of bottom 5% on just Cognitive VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*$bot5mean_cog) _tab 
	matrix SE[1,`counter'] = _se[ztva_cog_factor]*$bot5mean_cog
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 2: Deselection of bottom 5% on just Mixed VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_jackson_factor]*$bot5mean_mixed) _tab 
	matrix SE[1,`counter'] = _se[ztva_jackson_factor]*$bot5mean_mixed
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 3: Deselection of bottom 5% on just Noncognitive VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_noncog_factor]*$bot5mean_noncog) _tab 
	matrix SE[1,`counter'] = _se[ztva_noncog_factor]*$bot5mean_noncog
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 


*** Row 4: Deselection of bottom 5% using both Cognitive and Mixed VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*$bot5mean_cogandmixed_cog + _b[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed) _tab 
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*$bot5mean_cogandmixed_cog)^2 + (_se[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed)^2)
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 5: Deselection of bottom 5% using both Cognitive, Mixed, and Noncognitive VA 
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*$bot5mean_allthree_cog + _b[ztva_jackson_factor]*$bot5mean_allthree_mixed ///
		+ _b[ztva_noncog_factor]*$bot5mean_allthree_noncog) _tab 
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*$bot5mean_allthree_cog)^2 + (_se[ztva_jackson_factor]*$bot5mean_allthree_mixed)^2 ///
		+ (_se[ztva_noncog_factor]*$bot5mean_allthree_noncog)^2)
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 6: Deselection of bottom 5% using the optimal linear combitiona of Cognitive, Mixed, and Noncognitive VA for the given variable
matrix SE = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	file write tables (_b[ztva_cog_factor]*${opt_`var'_cog} + _b[ztva_jackson_factor]*${opt_`var'_mixed} ///
		+ _b[ztva_noncog_factor]*${opt_`var'_noncog}) _tab 
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*${opt_`var'_cog})^2 + (_se[ztva_jackson_factor]*${opt_`var'_mixed})^2 ///
		+ (_se[ztva_noncog_factor]*${opt_`var'_noncog})^2)
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 


cap file close tables 

restore

**********************************************************************
**** Table 23c: Deselection on Both True and Estimated Teacher VA ****
**********************************************************************
#delimit ;
local standard_va_vars "zcstmath zcstela";

local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

*** Create matrix with coeffificients of long run effects with all three factors (see table 7 in the code)
preserve 

use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

matrix COEFALL = J(3,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix COEFALL[1,`counter'] = _b[ztva_cog_factor]
	matrix COEFALL[2,`counter'] = _b[ztva_jackson_factor]
	matrix COEFALL[3,`counter'] = _b[ztva_noncog_factor]
	local ++counter
}

matrix list COEFALL

*** Correlation matrix between Cognitive, Mixed, and Noncognitive Factors
corr ztva_cog_factor ztva_jackson_factor ztva_noncog_factor if grade >= 3 & grade <= 5
matrix M = r(C)

restore

preserve

clear
set seed 123456
set obs 10000000

** Create all possible equally weighted linear combinations
drawnorm cog mixed noncog, corr(M)

gen allthree = (1/3)*cog + (1/3)*mixed + (1/3)*noncog
gen cogandmixed = (1/2)*cog + (1/2)*mixed
gen cogandnoncog = (1/2)*cog + (1/2)*mixed
gen mixedandnoncog = (1/2)*cog + (1/2)*mixed

** Create globals for the 5 percentile for each linear combination
sum allthree, detail
global p5_allthree = r(p5)

sum cogandmixed, detail
global p5_cogandmixed = r(p5)

sum cogandnoncog, detail
global p5_cogandnoncog = r(p5)

sum mixedandnoncog, detail
global p5_mixedandnoncog = r(p5)

foreach var in cog mixed noncog {
	sum `var', detail
	global p5_`var' = r(p5)	
}

*** Create the average number of standard deviations away from the mean that each factor is 
** for the bottom 5% of a given linear combination 
foreach var in cog mixed noncog { 
	sum `var' if allthree < $p5_allthree
	global bot5mean_allthree_`var' = abs(r(mean))
}

foreach var in cog mixed { 
	sum `var' if cogandmixed < $p5_cogandmixed
	global bot5mean_cogandmixed_`var' = abs(r(mean))
}

foreach var in cog noncog { 
	sum `var' if cogandnoncog < $p5_cogandnoncog
	global bot5mean_cogandnoncog_`var' = abs(r(mean))
}

foreach var in mixed noncog { 
	sum `var' if mixedandnoncog < $p5_mixedandnoncog
	global bot5mean_mixedandnoncog_`var' = abs(r(mean))
}

sum cog if cog < $p5_cog
global bot5mean_cog = abs(r(mean))

sum mixed if mixed < $p5_mixed
global bot5mean_mixed = abs(r(mean))

sum noncog if noncog < $p5_noncog
global bot5mean_noncog = abs(r(mean))

**** Create optimal linear combinations for row 5 of the deselection on true VA table (table 23a) 
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		gen opt_`var' = COEFALL[1,`counter']*cog + COEFALL[2,`counter']*mixed + COEFALL[3,`counter']*noncog
		sum opt_`var', detail 
		global opt_temp = r(p5)
		
		sum cog if opt_`var' < $opt_temp 
		global opt_`var'_cog = abs(r(mean))
		
		sum mixed if opt_`var' < $opt_temp 
		global opt_`var'_mixed = abs(r(mean))		
		
		sum noncog if opt_`var' < $opt_temp 
		global opt_`var'_noncog = abs(r(mean))
		
		local ++counter
}

restore 

*************************************************
**** Panel A: Deselection on True Teacher VA ****
*************************************************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

cap file close tables 
file open tables using "../output/table23c_deselectall.txt", write replace
file write tables "<tab:table23c_deselectall>" _n

*** Row 1: Deselection of bottom 5% on just Cognitive VA 
matrix SE = J(1,11,.)
matrix BETArow1 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE[1,`counter'] = _se[ztva_cog_factor]*$bot5mean_cog
	matrix BETArow1[1,`counter'] = _b[ztva_cog_factor]*$bot5mean_cog
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow1[1,`counter']/SE[1,`counter']))
	file write tables %9.3f (_b[ztva_cog_factor]*$bot5mean_cog) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 2: Deselection of bottom 5% on just Mixed VA 
matrix SE = J(1,11,.)
matrix BETArow2 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE[1,`counter'] = _se[ztva_jackson_factor]*$bot5mean_mixed
	matrix BETArow2[1,`counter'] = _b[ztva_jackson_factor]*$bot5mean_mixed
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow2[1,`counter']/SE[1,`counter']))
	file write tables %9.3f (_b[ztva_jackson_factor]*$bot5mean_mixed) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
		local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 


*** Row 3: Deselection of bottom 5% using both Cognitive and Mixed VA 
matrix SE = J(1,11,.)
matrix BETArow3 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*$bot5mean_cogandmixed_cog)^2 + (_se[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed)^2)
	matrix BETArow3[1,`counter'] = _b[ztva_cog_factor]*$bot5mean_cogandmixed_cog + _b[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow3[1,`counter']/SE[1,`counter']))
	file write tables %9.3f (_b[ztva_cog_factor]*$bot5mean_cogandmixed_cog + _b[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed) ///
		(cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab 
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

*** Row 4: Deselection of bottom 5% using the optimal linear combitiona of Cognitive, Mixed, and Noncognitive VA for the given variable
matrix SE = J(1,11,.)
matrix BETArow4 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE[1,`counter'] = sqrt((_se[ztva_cog_factor]*${opt_`var'_cog})^2 + (_se[ztva_jackson_factor]*${opt_`var'_mixed})^2 ///
		+ (_se[ztva_noncog_factor]*${opt_`var'_noncog})^2)
	matrix BETArow4[1,`counter'] = _b[ztva_cog_factor]*${opt_`var'_cog} + _b[ztva_jackson_factor]*${opt_`var'_mixed} + _b[ztva_noncog_factor]*${opt_`var'_noncog}
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow4[1,`counter']/SE[1,`counter']))
	file write tables %9.3f (_b[ztva_cog_factor]*${opt_`var'_cog} + _b[ztva_jackson_factor]*${opt_`var'_mixed} ///
		+ _b[ztva_noncog_factor]*${opt_`var'_noncog}) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab 
	local ++counter
}


file write tables _n 
forvalues x = 1/11 {
	file write tables (SE[1,`x']) _tab 
}
file write tables _n 

**** Row 5 
forvalues x = 1/11 {
	if 100*((BETArow3[1,`x'] - BETArow1[1,`x'])/BETArow1[1,`x']) > 200 | 100*((BETArow3[1,`x'] - BETArow1[1,`x'])/BETArow1[1,`x']) < -200 {
		file write tables "200%+" _tab
	}
	else {
		file write tables %9.0f (100*((BETArow3[1,`x'] - BETArow1[1,`x'])/BETArow1[1,`x'])) "%" _tab 
	}
}
file write tables _n 


********************************************************
**** Panel B: Deselection on Estimated Teacher VA ****
********************************************************
#delimit ;
local standard_va_vars "zcstmath zcstela";

local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

*** Create matrix with coeffificients of long run effects with all three factors (see table 7 in the code) 
preserve

use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15

matrix COEFALL = J(3,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix COEFALL[1,`counter'] = _b[ztva_cog_factor]
	matrix COEFALL[2,`counter'] = _b[ztva_jackson_factor]
	matrix COEFALL[3,`counter'] = _b[ztva_noncog_factor]
	local ++counter
}

matrix list COEFALL


* Create autocorrelation matrix for each VA factor  
use ../data/lausd_data/vascores/biastest_studentlevel, clear

keep if grade >= 2 & grade <= 5 
drop if teacherid == . 

*** Make teacher-year level dataset 
collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr{
		egen fn_`var' = std(score_r_`var')
}

gen score_r_cog_factor = fn_zcstmath + fn_zcstela
gen score_r_jackson_factor_even = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback
gen score_r_noncog_factor_even = fn_effmarktotalyr + fn_habmarkfirst5totalyr + fn_habmarklast9totalyr 

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

*** Create lagged 
foreach var in score_r_cog_factor score_r_jackson_factor score_r_noncog_factor {
*	score_r_zcstmath_math score_r_zcstela_english ///
*	score_r_gpatotal_math score_r_logdaysabsent_math score_r_suspended_math score_r_heldback_math /// 
*	score_r_gpatotal_english score_r_logdaysabsent_english score_r_suspended_english score_r_heldback_english
		gen l1`var' = L1.`var'
		gen l2`var' = L2.`var'
		gen l3`var' = L3.`var'
		gen l4`var' = L4.`var'
		gen l5`var' = L5.`var'
		gen l6`var' = L6.`var'
}

matrix AUTO_all = J(12,12,.)

forvalues x = 1/12 { 
	matrix AUTO_all[`x',`x'] = 1
}

local counter1 = 1 
foreach var1 in score_r_cog_factor l1score_r_cog_factor l2score_r_cog_factor l3score_r_cog_factor score_r_jackson_factor l1score_r_jackson_factor ///
	l2score_r_jackson_factor l3score_r_jackson_factor score_r_noncog_factor l1score_r_noncog_factor l2score_r_noncog_factor l3score_r_noncog_factor { 
		
		local counter2 = 1 
		foreach var2 in score_r_cog_factor l1score_r_cog_factor l2score_r_cog_factor l3score_r_cog_factor score_r_jackson_factor l1score_r_jackson_factor ///
		l2score_r_jackson_factor l3score_r_jackson_factor score_r_noncog_factor l1score_r_noncog_factor l2score_r_noncog_factor l3score_r_noncog_factor { 
			corr `var1' `var2' 
			matrix AUTO_all[`counter1',`counter2'] = r(rho)
			local ++counter2
		}
	local ++counter1
}

forvalues x = 1/12 { 
	matrix AUTO_all[`x',`x'] = 1
}

**** Monte Carlo Simulation to determine expected VA conditional on the estiamted average VA in the previous three is in the bottome 5%. 
clear
set seed 123456
set obs 10000000

drawnorm cog cog1 cog2 cog3 mixed mixed1 mixed2 mixed3 noncog noncog1 noncog2 noncog3, corr(AUTO_all)

gen estcog = (cog1 + cog2 + cog3)/3
gen estmixed = (mixed1 + mixed2 + mixed3)/3
gen estnoncog = (noncog1 + noncog2 + noncog3)/3


** Create all possible equally weighted linear combinations
gen allthree = (1/3)*estcog + (1/3)*estmixed + (1/3)*estnoncog
gen cogandmixed = (1/2)*estcog + (1/2)*estmixed
gen cogandnoncog = (1/2)*estcog + (1/2)*estmixed
gen mixedandnoncog = (1/2)*estcog + (1/2)*estmixed

** Create globals for the 5 percentile for each linear combination
sum allthree, detail
global p5_allthree = r(p5)

sum cogandmixed, detail
global p5_cogandmixed = r(p5)

sum cogandnoncog, detail
global p5_cogandnoncog = r(p5)

sum mixedandnoncog, detail
global p5_mixedandnoncog = r(p5)

foreach var in cog mixed noncog {
	sum est`var', detail
	global p5_`var' = r(p5)	
}

*** Create the average number of standard deviations away from the mean that each factor is 
** for the bottom 5% of a given linear combination 
foreach var in cog mixed noncog { 
	sum `var' if allthree < $p5_allthree
	global bot5mean_allthree_`var' = abs(r(mean))
}

foreach var in cog mixed { 
	sum `var' if cogandmixed < $p5_cogandmixed
	global bot5mean_cogandmixed_`var' = abs(r(mean))
}

foreach var in cog noncog { 
	sum `var' if cogandnoncog < $p5_cogandnoncog
	global bot5mean_cogandnoncog_`var' = abs(r(mean))
}

foreach var in mixed noncog { 
	sum `var' if mixedandnoncog < $p5_mixedandnoncog
	global bot5mean_mixedandnoncog_`var' = abs(r(mean))
}

sum cog if estcog < $p5_cog
global bot5mean_cog = abs(r(mean))

sum mixed if estmixed < $p5_mixed
global bot5mean_mixed = abs(r(mean))

sum noncog if estnoncog < $p5_noncog
global bot5mean_noncog = abs(r(mean))

**** Create optimal linear combinations for row 5 of the deselection on true VA table (table 23a) 
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		gen opt_`var' = COEFALL[1,`counter']*estcog + COEFALL[2,`counter']*estmixed + COEFALL[3,`counter']*estnoncog
		sum opt_`var', detail 
		global opt_temp = r(p5)
		
		sum cog if opt_`var' < $opt_temp 
		global opt_`var'_cog = abs(r(mean))
		
		sum mixed if opt_`var' < $opt_temp 
		global opt_`var'_mixed = abs(r(mean))		
		
		sum noncog if opt_`var' < $opt_temp 
		global opt_`var'_noncog = abs(r(mean))
		
		local ++counter
}
 

*** Create Table
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

*gen randnum = runiform()
*keep if randnum < 0.15


*** Row 1: Deselection of bottom 5% on just Cognitive VA 
matrix SE1 = J(1,11,.)
matrix BETArow1 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE1[1,`counter'] = _se[ztva_cog_factor]*$bot5mean_cog
	matrix BETArow1[1,`counter'] = _b[ztva_cog_factor]*$bot5mean_cog
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow1[1,`counter']/SE1[1,`counter']))
	file write tables %9.3f (_b[ztva_cog_factor]*$bot5mean_cog) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE1[1,`x']) _tab 
}
file write tables _n 


*** Row 2: Deselection of bottom 5% on just Mixed VA 
matrix SE2 = J(1,11,.)
matrix BETArow2 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE2[1,`counter'] = _se[ztva_jackson_factor]*$bot5mean_mixed
	matrix BETArow2[1,`counter'] = _b[ztva_jackson_factor]*$bot5mean_mixed
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow2[1,`counter']/SE2[1,`counter']))
	file write tables %9.3f (_b[ztva_jackson_factor]*$bot5mean_mixed) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab
		local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE2[1,`x']) _tab 
}
file write tables _n 

*** Row 3: Deselection of bottom 5% using both Cognitive and Mixed VA 
matrix SE3 = J(1,11,.)
matrix BETArow3 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE3[1,`counter'] = sqrt((_se[ztva_cog_factor]*$bot5mean_cogandmixed_cog)^2 + (_se[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed)^2)
	matrix BETArow3[1,`counter'] = _b[ztva_cog_factor]*$bot5mean_cogandmixed_cog + _b[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow3[1,`counter']/SE3[1,`counter']))
	file write tables %9.3f (_b[ztva_cog_factor]*$bot5mean_cogandmixed_cog + _b[ztva_jackson_factor]*$bot5mean_cogandmixed_mixed) ///
		(cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab 
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE3[1,`x']) _tab 
}
file write tables _n 


*** Row 4: Deselection of bottom 5% using the optimal linear combination of Cognitive, Mixed, and Noncognitive VA for the given variable
matrix SE4 = J(1,11,.)
matrix BETArow4 = J(1,11,.)
local counter = 1 
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	matrix SE4[1,`counter'] = sqrt((_se[ztva_cog_factor]*${opt_`var'_cog})^2 + (_se[ztva_jackson_factor]*${opt_`var'_mixed})^2 ///
		+ (_se[ztva_noncog_factor]*${opt_`var'_noncog})^2)
	matrix BETArow4[1,`counter'] = _b[ztva_cog_factor]*${opt_`var'_cog} + _b[ztva_jackson_factor]*${opt_`var'_mixed} + _b[ztva_noncog_factor]*${opt_`var'_noncog}
	
	if abs(BETArow3[1,`counter']) > abs(BETArow4[1,`counter']) { 
		matrix BETArow4[1,`counter'] = BETArow3[1,`counter']
		matrix SE4[1,`counter'] = SE3[1,`counter']
	}
	if abs(BETArow2[1,`counter']) > abs(BETArow4[1,`counter']) { 
		matrix BETArow4[1,`counter'] = BETArow2[1,`counter']
		matrix SE4[1,`counter'] = SE2[1,`counter']
	}
	if abs(BETArow1[1,`counter']) > abs(BETArow4[1,`counter']) { 
		matrix BETArow4[1,`counter'] = BETArow1[1,`counter']
		matrix SE4[1,`counter'] = SE1[1,`counter']
	}
	
	global pvalue = 2*ttail(e(N) - e(df_m),abs(BETArow4[1,`counter']/SE4[1,`counter']))
	file write tables %9.3f (BETArow4[1,`counter']) (cond($pvalue<.01,"***",cond($pvalue<.05,"**",cond($pvalue<.1,"*","")))) _tab 
	local ++counter
}

file write tables _n 
forvalues x = 1/11 {
	file write tables (SE4[1,`x']) _tab 
}
file write tables _n 

*** Row 5
forvalues x = 1/11 {
	if 100*((BETArow3[1,`x'] - BETArow1[1,`x'])/BETArow1[1,`x']) > 200 | 100*((BETArow3[1,`x'] - BETArow1[1,`x'])/BETArow1[1,`x']) < -200 {
		file write tables "200%+" _tab
	}
	else {
		file write tables %9.0f (100*((BETArow3[1,`x'] - BETArow1[1,`x'])/BETArow1[1,`x'])) "%" _tab 
	}
}
file write tables _n 


cap file close tables 

restore



***************************
*** TABLES For JPE R&R  ***
***************************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year
drop b_ztva* 
set matsize 11000
set more off
fvset base 2007 year
fvset base 3 grade

* Drop variables we don't need to speed boostrap
drop oth_* c_* r_* zcstsocial8 zcstsocial11 zcstsocialworld gpaart-apsemesters zcstscience5- habmarktotalep3 /// 
	habmarkauthoritytotalyr- habmarkuseoftimetotalyr ///
	gpalisten gparead gpaspeak gpawrite leavelausd apclasstotal 
drop gpascience2-gpaother3 tv* ztva_habmarkauthoritytotalyr-ztva_habmarkuseoftimetotalyr /// 
	ztva_gpascience-ztva_gpaart ztva_cog_factor_orig-ztva_agnostic4_factor_all	///
	ztva_allgpas_factor ztva_habits_factor-ztva_noncog_factor_wfe  noncog_factor_orig-noncog_factor_orig /// 
	ztva_cog_factor_post-ztva2l_noncog_factor_post
drop stdpseudoid tchpseudoidfrommarks psatscoreoverall dayssuspended numofsuspendions ///
	achmarktotalyr habmarktotalyr habmarkfirst5totalep1 habmarkfirst5totalep3 habmarkfirst5totalyr ///
	habmarklast9totalep1 habmarklast9totalep3 habmarklast9totalyr classid everpsat fracdaysabsent /// 
	hs_fracdaysabsent hs_suspended achmarkenglishtotalyr hs_leavelausd zgpatotal zeffmarktotalyr /// 
	zhabmarktotalyr attrition
drop grade_Lhabmarkfirst5totalyr class_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr /// 
	class_Lhabmarklast9totalyr grade_Lhabmarkfirst5totalep3 class_Lhabmarkfirst5totalep3 /// 
	grade_Lhabmarklast9totalep3 class_Lhabmarklast9totalep3  /// 
	habmarkfirst5totalyr2 grade_Lhabmarkfirst5totalyr2 class_Lhabmarkfirst5totalyr2 /// 
	habmarkfirst5totalyr3 grade_Lhabmarkfirst5totalyr3 class_Lhabmarkfirst5totalyr3 /// 
	habmarklast9totalyr2 grade_Lhabmarklast9totalyr2 class_Lhabmarklast9totalyr2 /// 
	habmarklast9totalyr3 grade_Lhabmarklast9totalyr3 class_Lhabmarklast9totalyr3 ///
	habmarkfirst5totalep32-class_Lhabmarklast9totalep33 gpaenglish? gpaenglish? 



#delimit ;
local standard_va_vars "zcstmath zcstela";


local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

	
local ctrls_ftrack "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year i.hs_trackmath_a i.hs_trackmath_b i.hs_trackmath_c i.hs_trackmath_r
	i.hs_apmath_classes i.hs_apenglish_classes i.hs_ela_elective i.hs_ela_elective";

local ctrls_secondary "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.zcstmath? 
	L.zcstela? L.gpatotal? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";	
	
local ctrls_split1 "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	ell i.grade i.year";
	
local ctrls_split2 "i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size)";

local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
local ctrls_ex_heldback "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";	
	
local ctrls_ex_suspend "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_monotonic "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath 
	L.zcstela L.gpatotal L.effmarktotalyr L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local controls_nonmonotonic "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent? L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent? grade_Lsuspended? grade_Lheldback? grade_Lell?
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent? class_Lsuspended? class_Lheldback? class_Lell? class_size?) 
	ell i.grade i.year";
	
	
	
#delimit cr

******************************************************************************
*** TABLE 66: Causal estimates using 3 factor variables - Put GPA in Cog Factor ***
******************************************************************************
cap file close tables 
file open tables using "../output/table66_gpacog_va.txt", write replace
file write tables "<tab:gpacog_va>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_allcog_factor ztva_inschool_factor ztva_noncog_factor ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table66_gpacog_va.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_allcog_factor ztva_inschool_factor ztva_noncog_factor)



******************************************************************************
*** TABLE 65: Graduation Robustness Check ***
******************************************************************************
cap file close tables 
file open tables using "../output/table65_graduation.txt", write replace
file write tables "<tab:graduation_va>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in evergrad evergrad12 evergrad11 evergrad10 evergrad9  { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table65_graduation.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)


**********************************************************
*** TABLE 64: Causal estimates using 3 factor variables ***
**********************************************************

preserve

* Obs from grades 3-11
gegen ctcog=count(ztva_cog_factor) if (grade>=4) & (grade<=11), by(studentid)
gegen ctjack=count(ztva_jackson_factor) if (grade>=4) & (grade<=11), by(studentid)
gen keeper=(ctcog!=.) & (ctjack!=.) & (ctjack>=7) & (ctcog>=7)
gegen maxkeeper =max(keeper), by(studentid)
keep if maxkeeper==1 
gegen avg_ztva_cog_factor=mean(ztva_cog_factor), by(studentid)
gegen avg_ztva_jackson_factor=mean(ztva_jackson_factor), by(studentid)

cap file close tables 
file open tables using "../output/table64_vamean.txt", write replace
file write tables "<tab:va_mean>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout evergrad eversat satscoretotal gpatotal12 workeffortgpatotal12 coopgpatotal12  ///
	zcstmath11 zcstela11 dayssuspended12 logdaysabsent12 heldback11 { 
	reg `var' avg_ztva_cog_factor avg_ztva_jackson_factor ///
		`ctrls_secondary' if grade == 4, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table64_vamean.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(avg_ztva_cog_factor avg_ztva_jackson_factor)

	
set scheme s1mono
twoway (hist avg_ztva_cog_factor, color(blue)) ///
      (hist avg_ztva_jackson_factor,  fcolor(none) lcolor(black)) ///
	  , legend(order(1 "Average Test-Score VA" 2 "Average Behavior VA" ))
graph export ../output/mean_va_grades_4_11.pdf, as(pdf)

restore


********************************************************************
*** TABLE 63: Bootstrap Standard Errors ***
********************************************************************
preserve
forvalues x=7/11{
	insheet using ../data/lausd_data/vascores/bootstrap`x'/bootstrapdata_highschoolblock_class.txt, clear
	if `x'>7{
		append using ../data/lausd_data/temp/boot_teacher_classid
	}
	save ../data/lausd_data/temp/boot_teacher_classid, replace
}
collapse (sd) _all
drop obs_*
foreach indvar in cog jackson noncog{
  foreach outvar in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		local `outvar'_`indvar' = `indvar'_`outvar'[1]
		di ``outvar'_`indvar''
  }
}
restore

foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	local `var'_bcog=_b[ztva_cog_factor]
	local `var'_secog=_se[ztva_cog_factor]
	local `var'_bjackson=_b[ztva_jackson_factor]
	local `var'_sejackson=_se[ztva_jackson_factor]
	local `var'_bnoncog=_b[ztva_noncog_factor]
	local `var'_senoncog=_se[ztva_noncog_factor]
}
clear
set obs 9
foreach indvar in cog jackson noncog{
  foreach outvar in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
    zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 	
	if "`indvar'"=="cog"{
		gen `outvar'=``outvar'_b`indvar'' if _n==1
		replace  `outvar'=``outvar'_se`indvar'' if _n==2
		replace `outvar'=``outvar'_`indvar'' if _n==3
	}
	if "`indvar'"=="jackson"{
		replace `outvar'=``outvar'_b`indvar'' if _n==4
		replace  `outvar'=``outvar'_se`indvar'' if _n==5
		replace `outvar'=``outvar'_`indvar'' if _n==6
	}
	
	if "`indvar'"=="noncog"{
		replace `outvar'=``outvar'_b`indvar'' if _n==7
		replace  `outvar'=``outvar'_se`indvar'' if _n==8
		replace `outvar'=``outvar'_`indvar'' if _n==9
	}
  }
}
cap file close tables 
file open tables using "../output/table63_boot_class_hs.txt", write replace
file write tables "<tab:va_boot_class_hs>" _n

forvalues x=1/9{
	file write tables (hs_dropout[`x']) _tab (eversat[`x']) _tab (satscoretotal[`x']) _tab (hs_gpatotal[`x']) _tab (hs_workeffortgpatotal[`x']) _tab (hs_coopgpatotal[`x']) _tab (zcahseescoremath[`x']) _tab (zcahseescoreela[`x']) _tab (hs_dayssuspended[`x']) _tab (hs_logdaysabsent[`x']) _tab (hs_heldback[`x']) _n
}
cap file close

	

********************************************************************
*** TABLE 62: Causal VA results using all variables - Single Var ***
********************************************************************
cap file close tables 
file open tables using "../output/table62_vasinglebig_sameyear.txt", write replace
file write tables "<tab:vasingle_bigsameyear>" _n
cap file close tables 

foreach indvar in ztva_zcstmath ztva_zcstela j_ztva_gpatotal j_ztva_habmarkfirst5totalyr ///
		j_ztva_habmarklast9totalyr j_ztva_effmarktotalyr /// 
		j_ztva_suspended j_ztva_logdaysabsent j_ztva_heldback{	
local counter = 1
local modellist = ""
 foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' `indvar' `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
  }

  estout `modellist' using ../output/table62_vasinglebig_sameyear.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(`indvar')
}

********************************************************************
*** TABLE 61: Causal VA results using all variables - Single Var ***
********************************************************************
cap file close tables 
file open tables using "../output/table61_vasinglebig.txt", write replace
file write tables "<tab:vasingle_big>" _n
cap file close tables 

foreach indvar in ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_habmarkfirst5totalyr ///
		ztva_habmarklast9totalyr ztva_effmarktotalyr /// 
		ztva_suspended ztva_logdaysabsent ztva_heldback{	
local counter = 1
local modellist = ""
 foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' `indvar' `ctrls' ///
			if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
  }

  estout `modellist' using ../output/table61_vasinglebig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(`indvar')
}



drop	ztva_zcstmath-ztva_habmarktotalyr ztva_effmarktotalyr-ztva_gpamat j_*

*****************************************************************************************
*** TABLE 60: Count of teachers by year***
*****************************************************************************************
preserve
collapse (count) studentid, by(teacherid grade cdscode year)
* drop teachers with very few students
drop if teacherid==. | studentid<5
collapse (count) teacherid, by( grade cdscode year)
tabstat teacherid, by(grade)
restore

*****************************************************************************************
*** TABLE 59: Causal estimates using 3 factor variables - Future Track ***
*****************************************************************************************
cap file close tables 
file open tables using "../output/table59_futuretrack.txt", write replace
file write tables "<tab:futuretrack>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls_ftrack'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table59_futuretrack.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)


******************************************************************************
*** TABLE 58: Causal estimates using 3 factor variables - Year t VA ***
******************************************************************************
cap file close tables 
file open tables using "../output/table58_yeart_va.txt", write replace
file write tables "<tab:yeart_va>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor_yrt ztva_noncog_factor_yrt ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table58_yeart_va.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor_yrt ztva_noncog_factor_yrt)


******************************************************************************
*** TABLE 57: Causal estimates using 3 factor variables - Teacher-Grade VA ***
******************************************************************************
cap file close tables 
file open tables using "../output/table57_teachergrade_va.txt", write replace
file write tables "<tab:teachergrade_va>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_tchgrade ztva_jackson_factor_tchgrade ztva_noncog_factor_tchgrade ///
		`ctrls'	if (grade >= 3) & (grade <= 4), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table57_teachergrade_va.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_tchgrade ztva_jackson_factor_tchgrade ztva_noncog_factor_tchgrade)

preserve
gen ct=1
collapse (mean) ztva_cog_factor_tchgrade ztva_jackson_factor_tchgrade ztva_noncog_factor_tchgrade ///
	[aw=ct],	by(teacherid grade)	
drop if grade>5
reshape wide ztva_cog_factor_tchgrade ztva_jackson_factor_tchgrade ztva_noncog_factor_tchgrade, ///
	i(teacherid) j(grade)

local cogname "Test-Score"
local jacksonname "Behavior"
local noncogname "Learning Skills"

foreach var in cog jackson noncog{
	corr ztva_`var'_factor_tchgrade3 ztva_`var'_factor_tchgrade4
	
	binscatter ztva_`var'_factor_tchgrade4 ztva_`var'_factor_tchgrade3, replace ///
		title(Grade 3 and 4 ``var'name' Value-Added) reportreg ///
		xtitle(Grade 3 ``var'name' Value-Added) ytitle(Grade 4 ``var'name' Value-Added) ///
		xlabel(-2(.5)2) ylabel(-2(.5)2)  savegraph(../output/ztva_`var'_tchgrade34.pdf)
	
}
restore

	
******************************************************************
*** TABLE 56: Causal estimates using 3 factor variables - ZGPA ***
******************************************************************
cap file close tables 
file open tables using "../output/table57_zgpa.txt", write replace
file write tables "<tab:zgpa>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_gpatotal hs_zgpatotal  hs_workeffortgpatotal hs_zworkeffortgpatotal hs_coopgpatotal  ///
	 hs_zcoopgpatotal { 
	reg `var' ztva_cog_factor ztva_jackson_factor_zgpa ztva_noncog_factor_zgpa ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table56_zgpa.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor_zgpa ztva_noncog_factor_zgpa)


*****************************************************************************************
*** TABLE 56: Causal estimates using 3 factor variables - No Learning Skills Controls ***
*****************************************************************************************
cap file close tables 
file open tables using "../output/table56_nolearning_ctrls.txt", write replace
file write tables "<tab:nolearning_ctrls>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor_nolrnctrl ztva_jackson_factor_nolrnctrl ztva_noncog_factor_nolrnctrl ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table56_nolearning_ctrls.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor_nolrnctrl ztva_jackson_factor_nolrnctrl ztva_noncog_factor_nolrnctrl)

**********************************************************************************
*** TABLE 55: Causal estimates using 3 factor variables - Monotonic Controls ***
**********************************************************************************
cap file close tables 
file open tables using "../output/table55_monotonic_ctrls.txt", write replace
file write tables "<tab:monotonic_ctrls>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`controls_monotonic'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table55_monotonic_ctrls.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)


**********************************************************************************
*** TABLE 54: Causal estimates using 3 factor variables - Non Monotonic Controls ***
**********************************************************************************
cap file close tables 
file open tables using "../output/table54_nonmonotonic_ctrls.txt", write replace
file write tables "<tab:nonmonotonic_ctrls>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`controls_nonmonotonic'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table54_nonmonotonic_ctrls.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)


****************************************************************************************
*** FIGURE: Held Back by Grade ***
****************************************************************************************
graph bar heldback, over(grade) title(Share Held Back by Grade) ytitle(Share Held Back) ///
 b1title(Grade)
graph export ../output/heldback_by_grade.pdf, replace

tabstat heldback, by(grade) stat(mean)


**********************************************************************************
*** TABLE 53: Causal estimates using 3 factor variables School-Cohort FE ***
**********************************************************************************
cap file close tables 
file open tables using "../output/table52_cohortsch_fe.txt", write replace
file write tables "<tab:school_cohort_fe>"_n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	areg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode) absorb(cohortsch)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table52_cohortsch_fe.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)



****************************************************************************************
*** TABLE 52: Next Year's Teacher  ***
****************************************************************************************
foreach var in cog jackson noncog{
	gen dist`var'=1 if L.`var'_factor!=.
	replace dist`var'=3 if (L.`var'_factor>1) & (L.`var'_factor<.) & (L.`var'_factor!=.)
	replace dist`var'=2 if (L.`var'_factor<-1) & (L.`var'_factor!=.)
}

cap file close tables 
file open tables using "../output/table52_tracktva.txt", write replace
file write tables "<tab:tracktva>" _n
cap file close tables 

cap log close
log using ./check.log, replace

foreach var in  cog jackson noncog { 
   	local counter = 1
	local modellist = ""
	areg F.ztva_`var'_factor c.ztva_`var'_factor##i.distcog ///
		if (grade >= 3) & (grade <= 4) , /// 
		cluster(hs_cdscode) absorb(cohortsch)
		
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
		
	lincom _b[2.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
	lincom _b[3.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]

	if "`var'"!="noncog"{
	
		areg F.ztva_`var'_factor c.ztva_`var'_factor##i.distcog ///
			if (grade >= 6) & (grade <= 7)  , /// 
			cluster(cdscode) absorb(cohortsch)
		estimate store model`counter'
		local modellist "`modellist' model`counter'"
		local ++counter
	
		lincom _b[2.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
		lincom _b[3.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]

		
		areg F.ztva_`var'_factor c.ztva_`var'_factor##i.distcog ///
			if (grade >= 9) & (grade <= 11) , /// 
			cluster(cdscode) absorb(cohortsch)
		estimate store model`counter'
		local modellist "`modellist' model`counter'"
		local ++counter
	
		lincom _b[2.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
		lincom _b[3.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
	}

	
	estout `modellist' using ../output/table52_tracktva.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N, labels(none) fmt(%9.0f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_`var'_factor 2.distcog#c.ztva_`var'_factor 3.distcog#c.ztva_`var'_factor 2.distcog 3.distcog)
	
}

***************************************************************************************
*** TABLE 52: Next Year's Teacher - Contemporaneous  ***
****************************************************************************************
preserve

foreach var in cog jackson noncog{
	gen dist`var'=1 if L.`var'_factor!=.
	replace dist`var'=3 if (L.`var'_factor>1) & (L.`var'_factor<.) & (L.`var'_factor!=.)
	replace dist`var'=2 if (L.`var'_factor<-1) & (L.`var'_factor!=.)
}

drop ztva_jackson_factor ztva_noncog_factor
ren ztva_jackson_factor_yrt ztva_jackson_factor  
ren ztva_noncog_factor_yrt ztva_noncog_factor 

cap file close tables 
file open tables using "../output/table52_tracktva_yeart.txt", write replace
file write tables "<tab:tracktva_yeart>" _n
cap file close tables 

cap log close
log using ./check.log, replace

foreach var in  cog jackson noncog { 
   	local counter = 1
	local modellist = ""
	reg F.ztva_`var'_factor c.ztva_`var'_factor##i.distcog `ctrls' ///
		if (grade >= 3) & (grade <= 4) , /// 
		cluster(hs_cdscode)
		
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
		
	lincom _b[2.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
	lincom _b[3.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
/*
	if "`var'"!="noncog"{
	
		reg F.ztva_`var'_factor c.ztva_`var'_factor##i.distcog `ctrls_secondary'  ///
			if (grade >= 6) & (grade <= 7)  , /// 
			cluster(cdscode) 
		estimate store model`counter'
		local modellist "`modellist' model`counter'"
		local ++counter
	
		lincom _b[2.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
		lincom _b[3.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]

		
		reg F.ztva_`var'_factor c.ztva_`var'_factor##i.distcog `ctrls_secondary' ///
			if (grade >= 9) & (grade <= 11) , /// 
			cluster(cdscode) 
		estimate store model`counter'
		local modellist "`modellist' model`counter'"
		local ++counter
	
		lincom _b[2.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
		lincom _b[3.distcog#c.ztva_`var'_factor]+_b[ztva_`var'_factor]
	}
*/
	
	estout `modellist' using ../output/table52_tracktva_yeart.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N, labels(none) fmt(%9.0f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) ///
	keep(ztva_`var'_factor 2.distcog#c.ztva_`var'_factor 3.distcog#c.ztva_`var'_factor 2.distcog 3.distcog)
	
}
restore

****************************************************************************************
*** TABLE 51: Causal estimates using 3 factor variables - t+1 test score value added ***
****************************************************************************************
cap file close tables 
file open tables using "../output/table51_ftestscoreva.txt", write replace
file write tables "<tab:ftestscoreva>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_fcog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if (grade >= 3) & (grade <= 5), /// 
		cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table51_ftestscoreva.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_fcog_factor ztva_jackson_factor ztva_noncog_factor)


**********************************************************************************
*** TABLE 50: Causal estimates using 3 factor variables - Drop Suspend/Heldback ***
**********************************************************************************
cap file close tables 
file open tables using "../output/table50_noheld_supsend.txt", write replace
file write tables "<tab:noheld_suspend>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if (grade >= 3) & (grade <= 5) & (suspended==0) & (heldback==0), /// 
		cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table50_noheld_supsend.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)


************************************************************************************
*** Absences Summary Stats ***
************************************************************************************
*gen daysabsent=exp(logdaysabsent)-1
*sum daysabsent if grade>=3 & grade<=5, det


************************************************************************************
*** MECHANICAL EFFECTS ***
************************************************************************************

*reg zcstmath suspended if grade >= 3 & grade <= 5, cluster(hs_cdscode)
*reg zcstmath suspended  `ctrls_ex_suspend' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
*reg F.gpatotal suspended  if grade >= 3 & grade <= 5, cluster(hs_cdscode)
reg F.gpatotal suspended  `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)

*reg zcstmath heldback if grade >= 3 & grade <= 5, cluster(hs_cdscode)
*reg zcstmath heldback `ctrls_ex_heldback' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
*reg F.gpatotal heldback if grade >= 3 & grade <= 5, cluster(hs_cdscode)
reg F.gpatotal heldback `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)


sum gpatotal zcstmath if grade >= 3 & grade <= 5


************************************************************************************
*** TABLE 45: Causal estimates using 3 factor variables, Cluster at school level ***
************************************************************************************

cap file close tables 
file open tables using "../output/table45_clustersch.txt", write replace
file write tables "<tab:va_clustersch>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table45_clustersch.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)

************************************************************************************
*** TABLE 45: Causal estimates using 3 factor variables, Cluster at Cohort ***
************************************************************************************
cap file close tables 
file open tables using "../output/table45_clustercohort.txt", write replace
file write tables "<tab:va_clustercohort>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(cohortsch) 
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table45_clustercohort.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor)

************************************************************************************
*** TABLE 45: Causal estimates using 1 factor variables                          ***
************************************************************************************
cap file close tables 
file open tables using "../output/table45_singlefactor.txt", write replace
file write tables "<tab:va_singlefactor>" _n
cap file close tables 

local counter = 1
foreach indvar in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
local modellist = ""
	foreach depvar in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
		zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
		reg `depvar' `indvar' `ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode) 
		estimate store model`counter'
		local modellist "`modellist' model`counter'"
		local ++counter
	}
	estout `modellist' using ../output/table45_singlefactor.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
		stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(`indvar')
}
	



*restore
********************
***  FIGURES     ***
********************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

set scheme s2color

fvset base 2007 year
fvset base 3 grade

************************************************************
**** Binscatter teacher factor VA by Actual HS outcome *****
************************************************************


** Create grade 11 math and Enligh CST scores
egen tempvar = mean(zcstmath), by(studentid grade)
replace tempvar = . if grade != 11
egen zcstmathg11 = mean(tempvar), by(studentid)
drop tempvar 

egen tempvar = mean(zcstela), by(studentid grade)
replace tempvar = . if grade != 11
egen zcstelag11 = mean(tempvar), by(studentid)
drop tempvar 

** Create postition local for each variable-factor-(actual, predicted, or both) binscatter graph
** Set the defalt position for each graph to 5 
foreach var in evergrad hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 zcstelag11 zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
   		local fac = substr("`factor'", 6, 3)
		local pa_`var'_`fac' = 5
		local pp_`var'_`fac' = 5
		local pb_`var'_`fac' = 5
		
	}
}

** Changes for actual 
local pa_evergrad_cog = 1
local pa_eversat_cog = 11
local pa_hs_logdaysabsent_cog = 5
local pa_hs_dropout_jac = 5
local pa_hs_dayssuspended_jac = 1
local pa_hs_fracdaysabsent_jac = 1
local pa_hs_heldback_jac = 1
local pa_apclasstotal_jac = 11
local pa_hs_logdaysabsent_jac = 1
local pa_hs_dayssuspended_non = 1
local pa_hs_fracdaysabsent_non = 1
local pa_hs_heldback_non = 5
local pa_everpsat_non = 11
local pa_hs_dropout_non = 5
local pa_hs_logdaysabsent_non = 1
local pa_hs_workeffortgpatotal_non = 1
local pa_hs_coopgpatotal_non = 7

** Changes for predicted
local pp_hs_dayssuspended_cog = 1 
local pp_hs_fracdaysabsent_cog = 1
local pp_hs_heldback_cog = 1
local pp_hs_logdaysabsent_cog = 1
local pp_evergrad_jac = 1
local pp_hs_gpatotal_jac = 1 
local pp_psatscoreoverall_jac = 11
local pp_zcstscience5_jac = 1
local pp_zcstscience8_jac = 1
local pp_zcstsocial8_jac = 1
local pp_zcstsocial11_jac = 1
local pp_zcstsocialworld_jac = 1
local pp_evergrad_non = 1
local pp_psatscoreoverall_non = 1

** Changes for both 
local pb_evergrad_cog = 1
local pb_eversat_cog = 11
local pb_hs_dropout_cog = 7
local pb_hs_heldback_cog = 11
local pb_hs_logdaysabsent_cog = 1
local pb_apclasstotal_jac = 11
local pb_everpsat_jac = 7
local pb_hs_dayssuspended_jac = 1
local pb_hs_dropout_jac = 7 
local pb_fracdaysabsent_jac = 1 
local pb_hs_heldback_jac = 1
local pb_hs_logdaysabsent_jac = 1
local pb_everpsat_non = 7
local pb_hs_dayssuspended_non = 1
local pb_fracdaysabsent_non = 1
local pb_hs_heldback_non = 1
local pb_hs_logdaysabsent_non = 1	

** Variable Labels
label var evergrad "HS Graduate" 
label var hs_gpatotal "HS GPA"
label var eversat "Took SAT"
label var satscoretotal "SAT Score"
label var hs_dayssuspended "Days Suspended in HS" 
label var hs_fracdaysabsent "Fraction Days Absent in HS"
label var hs_logdaysabsent "Log Days Absent in HS"
label var hs_heldback "Heldback in HS"
label var zcahseescoremath "CAHSEE Math Score"
label var zcahseescoreela "CAHSEE English Score" 
label var hs_dropout "HS Dropout" 
label var apclasstotal "Number of AP Courses" 
label var zcstscience10 "Grade 10 Science CST Score" 
label var zcstsocial11 "Grade 11 Social Science CST Score" 
label var hs_workeffortgpatotal "HS Effort GPA"
label var hs_coopgpatotal "HS Cooperation GPA"
label var everpsat "Took PSAT"
label var psatscoreoverall "PSAT Score"
label var zcstmath "Math CST Score"
label var zcstela "English CST Score"
label var zcstsocial8 "Grade 8 Social Science CST Score" 
label var zcstsocialworld "World History CST Score" 
label var zcstscience5 "Grade 5 Science CST Score"
label var zcstscience8 "Grade 8 Science CST Score"
label var zcstmathg11 "Grade 11 Math CST Score"
label var zcstelag11 "Grade 11 English CST Score"

label var ztva_cog_factor "Test Score Value-Added"
label var ztva_fcog_factor "Test Score Value-Added (t+1)"
label var ztva_jackson_factor "Behavior Value-Added"
label var ztva_noncog_factor "Learning Skills Value-Added"

*** note("Coefficient = `coeff'%" "(`se')", position(5) ring(0) size(medium))
*** if "`var'" == "incar_totalmin"
*** legend(order("white sex ratio" 2 "black sex ratio"), postition(10) ring(0))
*legend(on order(- "asdfas") pos(11) ring(0)) ///

#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

preserve

keep year studentid teacherid grade evergrad hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 zcstelag11 zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 /// 
	ztva_fcog_factor ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
	hs_cdscode zcstmath* zcstela* gpatotal* effmarktotalyr* zcstmath* logdaysabsent* suspended* heldback* grade_* class_* ell

*** Binscatter plots
foreach depvar in evergrad hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 zcstelag11 zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
	foreach factor in ztva_fcog_factor ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
	   	reg `depvar' `factor' `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	   	local stars ""
		local space ""
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.1 {
			local stars "*"
			local space " "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.05 {
			local stars "**"
			local space "  "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.01 {
			local stars "***"
			local space "    "
		}
		local tempcoef: display %5.3fc _b[`factor']
	   	di `tempcoef'
	   	local tempse: display %5.3fc _se[`factor'] 
	   	di `tempse'
	   	local fac = substr("`factor'", 6, 3)
		binscatter `depvar' `factor' if grade >= 3 & grade <= 5, controls(`ctrls') mcolors(black) lcolors(black) ///
	   		ytitle(`: variable label `depvar'', color(black)) xtitle(`: variable label `factor'', color(black)) ///
	   		title(`: variable label `depvar'', color(black)) ylabel(,nogrid) xlabel(-2(1)2) ///
			graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
	    	note("Coef = `tempcoef'`stars' " "(`tempse')`space'", box fcolor(none) placement(`pa_`depvar'_`fac'') ring(0) justification(right) size(large)) ///
	    	savegraph(../output/binscatter_`factor'_`depvar'.eps) savedata(../output/binscatterdata/actual_`factor'_`depvar'.dta) replace 
	}
}	   
restore

	  /* 	

***************************************************************
**** Binscatter teacher factor VA by Predicted HS outcome *****
***************************************************************
#delimit ;

local tchtotal_l1controls "i.grade#C.(L2.zcstmath L2.zcstela L2.gpatotal L2.effmarktotalyr L2.zcstmath? 
	L2.zcstela? L2.gpatotal? L2.effmarktotalyr? L2.logdaysabsent L2.suspended L2.heldback) 
	L.ell i.L.grade i.L.year";
	
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

*** Binscatter Plots
foreach depvar in /*evergrad*/ hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 /*zcstelag11*/ zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
		reg `depvar' `tchtotal_l1controls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
		predict resid_`depvar' if e(sample) == 1, xb
		
		foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
			reg resid_`depvar' `factor' `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
			local stars ""
			local space ""
			if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.1 {
				local stars "*"
				local space " "
			}
			if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.05 {
				local stars "**"
				local space "  "
			}
			if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.01 {
				local stars "***"
				local space "    "
			}
			local tempcoef: display %5.3fc _b[`factor']
			di `tempcoef'
			local tempse: display %5.3fc _se[`factor'] 
			di `tempse'
			binscatter resid_`depvar' `factor' if grade >= 3 & grade <= 5, controls(`ctrls') mcolors(black) lcolors(black) ///
		   		ytitle(`: variable label `depvar'', color(black)) xtitle(`: variable label `factor'', color(black)) ///
		    	title(`: variable label `depvar'', color(black)) ylabel(,nogrid) xlabel(-2(1)2) ///
		    	note("Coef = `tempcoef'`stars' " "(`tempse')`space'", box fcolor(none) placement(`pp_`depvar'_`fac'') ring(0) justification(right) size(large)) ///
		    	savegraph(../output/predicted_binscatter_`factor'_`depvar'.png) savedata(../output/binscatterdata/predicted_`factor'_`depvar'.dta) replace 
	}
}	   	

**************************************************************************
**** Binscatter teacher factor VA by Actual and Predicted HS outcome *****
**************************************************************************
******** Binscatter for the actual and predicted outcomes using exported data 
#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr


preserve

foreach depvar in /*evergrad*/ hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 /*zcstelag11*/ zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
		insheet using "../output/binscatterdata/actual_`factor'_`depvar'.dta.csv", comma clear
		save "../output/binscatterdata/actual_`factor'_`depvar'.dta", replace 
		
		insheet using "../output/binscatterdata/predicted_`factor'_`depvar'.dta.csv", comma clear
		save "../output/binscatterdata/predicted_`factor'_`depvar'.dta", replace 
	}
}
restore
		
		
** Binscatter Plot		
foreach depvar in /*evergrad*/ hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 /*zcstelag11*/ zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {	
		
		reg `depvar' `factor' `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	   	local stars ""
		local space ""
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.1 {
			local stars "*"
			local space " "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.05 {
			local stars "**"
			local space "  "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.01 {
			local stars "***"
			local space "    "
		}
		local tempcoef: display %5.3fc _b[`factor']
	   	di `tempcoef'
	   	local tempse: display %5.3fc _se[`factor'] 
	   	di `tempse'
		
		preserve
		
		use "../output/binscatterdata/predicted_`factor'_`depvar'.dta", clear
		rename resid_`depvar' `depvar'
		append using "../output/binscatterdata/actual_`factor'_`depvar'.dta", generate(actualdata)
		
		cap label var evergrad "HS Graduate" 
		cap label var hs_gpatotal "HS GPA"
		cap label var eversat "Took SAT"
		cap label var satscoretotal "SAT Score"
		cap label var hs_dayssuspended "Days Suspended in HS" 
		cap label var hs_fracdaysabsent "Fraction Days Absent in HS"
		cap label var hs_logdaysabsent "Log Days Absent in HS"
		cap label var hs_heldback "Heldback in HS"
		cap label var zcahseescoremath "CAHSEE Math Score"
		cap label var zcahseescoreela "CAHSEE English Score" 
		cap label var hs_dropout "HS Dropout" 
		cap label var apclasstotal "Number of AP courses" 
		cap label var zcstscience10 "Grade 10 Science CST Score" 
		cap label var zcstsocial11 "Grade 11 Social Science CST Score" 
		cap label var hs_workeffortgpatotal "HS Effort GPA"
		cap label var hs_coopgpatotal "HS Cooperation GPA"
		cap label var everpsat "Took PSAT"
		cap label var psatscoreoverall "PSAT Score"
		cap label var zcstmath "Math CST Score"
		cap label var zcstela "English CST Score"
		cap label var zcstsocial8 "Grade 8 Social Science CST Score" 
		cap label var zcstsocialworld "World History CST Score" 
		cap label var zcstscience5 "Grade 5 Science CST Score"
		cap label var zcstscience8 "Grade 8 Science CST Score"
		cap label var zcstmathg11 "Grade 11 Math CST Score"
		cap label var zcstelag11 "Grade 11 English CST Score"

		cap label var ztva_cog_factor "Test Score Value-Added"
		cap label var ztva_jackson_factor "Behavior Value-Added"
		cap label var ztva_noncog_factor "Learning Skills Value-Added"
		
		graph twoway (scatter `depvar' `factor' if actualdata == 0, mcolor(gs10) lcolor(gs10) msymbol(diamond)) ///
			(scatter `depvar' `factor' if actualdata == 1, mcolor(black) lcolor(black) msymbol(circle)) ///
			(lfit `depvar' `factor' if actualdata == 0, lpattern(dash) lcolor(gs10)) ///
			(lfit `depvar' `factor' if actualdata == 1, lpattern(solid) lcolor(black)), ///
			legend(order(1 2) lab(1 Predicted) lab(2 Actual)) graphregion(color(white)) ///
			note("Coef = `tempcoef'`stars' " "(`tempse')`space'", box fcolor(none) placement(`pb_`depvar'_`fac'') ring(0) justification(right) size(large)) ///
	    	title(`: variable label `depvar'', color(black)) ylabel(,nogrid) xlabel(-2(1)2) ///
	    	ytitle(`: variable label `depvar'', color(black)) xtitle(`: variable label `factor'', color(black))  
		graph export "../output/both2sep_binscatter_`factor'_`depvar'.png", replace

		restore

	}
}
	*/	
*******************************************************************
***** Program to plot the dynamic effect of teacher factor VA *****
*******************************************************************
cap program drop plotdynamic
program plotdynamic
	syntax [anything] [if] , depvar(string) plotvar(string) controls_lead(string) controls_lag(string) cluster(string) /// 
	factornames(string) leads(string) title(string) ytitle(string) xtitle(string) output(string)
	cap matrix drop MSAVE
	local nplot=wordcount("`plotvar'")
	local x = `nplot'*3
	local y = `leads'+2
	
	local counter =1
	foreach var in `plotvar'{
		local pvar`counter' = "`var'"
		local ++counter
	}
	local counter =1
	foreach var in `factornames'{
		local fvar`counter' = "`var'"
		local ++counter
	}
	
	*** These two lines of code are needed just if the first variable has two words in it instead of one
	local fvar1 = "`fvar1' `fvar2'"
	local fvar2 = "`fvar3'"
	local fvar3 = "`fvar4'"
	
	
	di "`pvar1'"
	
	matrix MSAVE = J(`y',`x',.)
	
	local leadcounter = 1
		reg L.`depvar' `plotvar' `controls_lag' `if', cluster(`cluster') 
			matrix MSAVE[`leadcounter',1] = _b[`pvar1'] 
			matrix MSAVE[`leadcounter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`leadcounter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`leadcounter',4] = _b[`pvar2'] 
				matrix MSAVE[`leadcounter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`leadcounter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`leadcounter',7] = _b[`pvar3'] 
				matrix MSAVE[`leadcounter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`leadcounter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++leadcounter

		
	forvalues lead = 0/`leads'{
		reg F(`lead').`depvar' `plotvar' `controls_lead' `if', cluster(`cluster') 
			matrix MSAVE[`leadcounter',1] = _b[`pvar1'] 
			matrix MSAVE[`leadcounter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`leadcounter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`leadcounter',4] = _b[`pvar2'] 
				matrix MSAVE[`leadcounter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`leadcounter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`leadcounter',7] = _b[`pvar3'] 
				matrix MSAVE[`leadcounter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`leadcounter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++leadcounter
		}
	preserve
	clear
	svmat MSAVE
	gen relyear = _n-2
	drop if relyear == -1
	
	gen relyear2 = relyear - 0.04
	gen relyear3 = relyear + 0.04
	
	if `nplot'==1{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black) ///
			yline(0, lcolor(gs8)) xlabel(0(1)`leads') ylabel(,nogrid) ///
			legend(off) graphregion(color(white)) lcolor(black) title(`title') ytitle(`ytitle') xtitle(`xtitle'))
	}
	if `nplot'==2{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black) ///
			yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			lcolor(black) graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(0(1)`leads') ///
			legend(order(1 2) label(1 "`fvar1'") label(2 "`fvar2'")))

	}
	if `nplot'==3{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(dot)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(connected MSAVE7 relyear3, mcolor(black) msize(small) lcolor(black) msymbol(triangle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black)) ///
			(rcap  MSAVE8 MSAVE9 relyear3, lcolor(black) yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(0(1)`leads') ///
			legend(order(1 2 3) label(1 "`fvar1'") label(2 "`fvar2'") label(3 "`fvar3'") cols(3)))
	}
	graph export `output', replace
		
	restore
end


#delimit ;
local ctrls_lead "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local ctrls_lag "i.L.grade#(C.(L2.zcstmath L2.zcstela L2.gpatotal L2.effmarktotalyr L2.zcstmath? 
	L2.zcstela? L2.gpatotal? L2.effmarktotalyr? L2.logdaysabsent L2.suspended L2.heldback))
	i.L.grade#C.(L.grade_Lzcstmath L.grade_Lzcstela L.grade_Lgpatotal L.grade_Leffmarktotalyr 
	L.grade_Lzcstmath? L.grade_Lzcstela? L.grade_Lgpatotal? L.grade_Leffmarktotalyr? 
	L.grade_Llogdaysabsent L.grade_Lsuspended L.grade_Lheldback grade_Lell
	L.class_Lzcstmath L.class_Lzcstela L.class_Lgpatotal L.class_Leffmarktotalyr 
	L.class_Lzcstmath? L.class_Lzcstela? L.class_Lgpatotal? L.class_Leffmarktotalyr? 
	L.class_Llogdaysabsent L.class_Lsuspended L.class_Lheldback L.class_Lell L.class_size) 
	L.ell i.L.grade i.L.year";
	
local ctrls_small "L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size 
	ell i.grade i.year";	
#delimit cr

*************************************
*** Dynamic Figures using Factors ***
*************************************
plotdynamic if grade >= 3 & grade <= 5, depvar(zcstmath) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(Math Test Scores) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_zcstmath.eps)

plotdynamic if grade >= 3 & grade <= 5, depvar(zcstela) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(English Test Scores) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_zcstela.eps)	

plotdynamic if grade >= 3 & grade <= 5, depvar(gpatotal) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_zgpa.eps)

plotdynamic if grade >= 3 & grade <= 5, depvar(effmarktotalyr) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_small') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(3) title(Effort GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_effmarktotalyr.eps)	
	
plotdynamic, depvar(zhabmarktotalyr) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(3) title(Habits GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_zhabmarktotalyr.eps)	
	
plotdynamic if grade >= 3 & grade <= 5, depvar(logdaysabsent) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(Log Absences) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_logabsent.eps)	
	
plotdynamic if grade >= 3 & grade <= 5, depvar(fracdaysabsent) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(Fraction Absent) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_fracdaysabsen.eps)	

plotdynamic if grade >= 3 & grade <= 5, depvar(suspended) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(Suspensions) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_suspended.eps)	

plotdynamic if grade >= 3 & grade <= 5, depvar(heldback) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(Being Held Back) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_heldback.eps)		

plotdynamic if grade >= 3 & grade <= 5, depvar(gpatotal) plotvar(ztva_cog_factor ztva_jackson_factor_contemp ztva_noncog_factor_contemp) ///
	factornames(Cognitive Mixed_Contemp Noncognitive_Contemp) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_zgpa_contemp.eps)





/*
************************
*** Attrition Graphs ***
************************
*******************************************************************
***** Program to plot attrition dynamic effects of teacher factor VA by lead years *****
*******************************************************************
cap program drop plotdynamic
program plotdynamic
	syntax [anything] [if] , depvar(string) plotvar(string) controls_lead(string) controls_lag(string) cluster(string) /// 
	factornames(string) leads(string) title(string) ytitle(string) xtitle(string) output(string)
	cap matrix drop MSAVE
	local nplot=wordcount("`plotvar'")
	local x = `nplot'*3
	local y = `leads'+2
	
	local counter =1
	foreach var in `plotvar'{
		local pvar`counter' = "`var'"
		local ++counter
	}
	local counter =1
	foreach var in `factornames'{
		local fvar`counter' = "`var'"
		local ++counter
	}
	
	*** These two lines of code are needed just if the first variable has two words in it instead of one
	local fvar1 = "`fvar1' `fvar2'"
	local fvar2 = "`fvar3'"
	local fvar3 = "`fvar4'"
	
	
	di "`pvar1'"
	
	matrix MSAVE = J(`y',`x',.)
	
	local leadcounter = 1
		reg L.`depvar' `plotvar' `controls_lag' `if', cluster(`cluster') 
			matrix MSAVE[`leadcounter',1] = _b[`pvar1'] 
			matrix MSAVE[`leadcounter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`leadcounter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`leadcounter',4] = _b[`pvar2'] 
				matrix MSAVE[`leadcounter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`leadcounter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`leadcounter',7] = _b[`pvar3'] 
				matrix MSAVE[`leadcounter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`leadcounter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++leadcounter

		
	forvalues lead = 0/`leads'{
		reg F(`lead').`depvar' `plotvar' `controls_lead' `if', cluster(`cluster') 
			matrix MSAVE[`leadcounter',1] = _b[`pvar1'] 
			matrix MSAVE[`leadcounter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`leadcounter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`leadcounter',4] = _b[`pvar2'] 
				matrix MSAVE[`leadcounter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`leadcounter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`leadcounter',7] = _b[`pvar3'] 
				matrix MSAVE[`leadcounter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`leadcounter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++leadcounter
		}
	preserve
	clear
	svmat MSAVE
	gen relyear = _n-2
	drop if relyear == -1
	
	gen relyear2 = relyear - 0.04
	gen relyear3 = relyear + 0.04
	
	if `nplot'==1{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black) ///
			yline(0, lcolor(gs8)) xlabel(0(1)`leads') ylabel(,nogrid) ///
			legend(off) graphregion(color(white)) lcolor(black) title(`title') ytitle(`ytitle') xtitle(`xtitle'))
	}
	if `nplot'==2{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black) ///
			yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			lcolor(black) graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(0(1)`leads')  ylabel(-0.01(.005)0.01) ///
			legend(order(1 2) label(1 "`fvar1'") label(2 "`fvar2'")))

	}
	if `nplot'==3{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(dot)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(connected MSAVE7 relyear3, mcolor(black) msize(small) lcolor(black) msymbol(triangle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black)) ///
			(rcap  MSAVE8 MSAVE9 relyear3, lcolor(black) yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(0(1)`leads')  ylabel(-0.01(.005)0.01) ///
			legend(order(1 2 3) label(1 "`fvar1'") label(2 "`fvar2'") label(3 "`fvar3'") cols(3)))
	}
	graph export `output', replace
		
	restore
end


#delimit ;
local ctrls_lead "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

local ctrls_lag "i.L.grade#(C.(L2.zcstmath L2.zcstela L2.gpatotal L2.effmarktotalyr L2.zcstmath? 
	L2.zcstela? L2.gpatotal? L2.effmarktotalyr? L2.logdaysabsent L2.suspended L2.heldback))
	i.L.grade#C.(L.grade_Lzcstmath L.grade_Lzcstela L.grade_Lgpatotal L.grade_Leffmarktotalyr 
	L.grade_Lzcstmath? L.grade_Lzcstela? L.grade_Lgpatotal? L.grade_Leffmarktotalyr? 
	L.grade_Llogdaysabsent L.grade_Lsuspended L.grade_Lheldback grade_Lell
	L.class_Lzcstmath L.class_Lzcstela L.class_Lgpatotal L.class_Leffmarktotalyr 
	L.class_Lzcstmath? L.class_Lzcstela? L.class_Lgpatotal? L.class_Leffmarktotalyr? 
	L.class_Llogdaysabsent L.class_Lsuspended L.class_Lheldback L.class_Lell L.class_size) 
	L.ell i.L.grade i.L.year";
	
local ctrls_small "L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size 
	ell i.grade i.year";	
#delimit cr

*************************************
*** Lead Years Attrition Graphs ***
*************************************
plotdynamic if grade >= 3 & grade <= 5, depvar(attrition) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead(`ctrls_lead') controls_lag(`ctrls_lag') ///
	cluster(hs_cdscode) leads(7) title(Leads of Student Attrition) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/dynfactor_attrition.png)

**********************************************************************************
***** Program to plot attrition dynamic effect of teacher factor VA by future grade *****
**********************************************************************************
cap program drop plotgradedynamic
program plotgradedynamic
	syntax [anything] [if] , depvar(string) plotvar(string) controls(string) cluster(string) /// 
	factornames(string) grades(string) title(string) ytitle(string) xtitle(string) output(string)
	cap matrix drop MSAVE
	local nplot=wordcount("`plotvar'")
	local x = `nplot'*3
	local y = `grades'+1
	
	local counter =1
	foreach var in `plotvar'{
		local pvar`counter' = "`var'"
		local ++counter
	}
	local counter =1
	foreach var in `factornames'{
		local fvar`counter' = "`var'"
		local ++counter
	}
	
	*** These two lines of code are needed just if the first variable has two words in it instead of one
	local fvar1 = "`fvar1' `fvar2'"
	local fvar2 = "`fvar3'"
	local fvar3 = "`fvar4'"
	
	
	di "`pvar1'"
	
	matrix MSAVE = J(`y',`x',.)
	local gradecounter=1
	local startgrade=5
	local endgrade=`startgrade'+`grades'
	forvalues grade = 5/`endgrade'{
		gen `depvar'_g`grade' = `depvar' if grade==`grade'
		egen e`depvar'_g`grade' = mean(`depvar'_g`grade'), by(studentid)
		reg e`depvar'_g`grade' `plotvar' `controls' `if', cluster(`cluster') 
			matrix MSAVE[`gradecounter',1] = _b[`pvar1'] 
			matrix MSAVE[`gradecounter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`gradecounter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`gradecounter',4] = _b[`pvar2'] 
				matrix MSAVE[`gradecounter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`gradecounter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`gradecounter',7] = _b[`pvar3'] 
				matrix MSAVE[`gradecounter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`gradecounter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++gradecounter
			drop e`depvar'_g`grade' `depvar'_g`grade'
		}
	preserve
	clear
	svmat MSAVE
	gen relyear = _n+`startgrade'-1
	drop if relyear == -1
	
	gen relyear2 = relyear - 0.04
	gen relyear3 = relyear + 0.04
	
	if `nplot'==1{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black) ///
			yline(0, lcolor(gs8)) xlabel(`startgrade'(1)`endgrade') ylabel(,nogrid) ///
			legend(off) graphregion(color(white)) lcolor(black) title(`title') ytitle(`ytitle') xtitle(`xtitle'))
	}
	if `nplot'==2{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black) ///
			yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			lcolor(black) graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(`startgrade'(1)`endgrade') ylabel(-0.01(.005)0.01) ///
			legend(order(1 2) label(1 "`fvar1'") label(2 "`fvar2'")))

	}
	if `nplot'==3{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(dot)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(connected MSAVE7 relyear3, mcolor(black) msize(small) lcolor(black) msymbol(triangle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black)) ///
			(rcap  MSAVE8 MSAVE9 relyear3, lcolor(black) yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(`startgrade'(1)`endgrade') ylabel(-0.01(.005)0.01) ///
			legend(order(1 2 3) label(1 "`fvar1'") label(2 "`fvar2'") label(3 "`fvar3'") cols(3)))
	}
	graph export `output', replace
		
	restore
end


#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

*****************************************************
*** Attrition Graphs by by future grade ***
*****************************************************
plotgradedynamic if grade >= 3 & grade <= 5, depvar(attrition) plotvar(ztva_cog_factor ztva_jackson_factor ztva_noncog_factor) ///
	factornames(Test Score Behavior Learning) ///
	controls(`ctrls') ///
	cluster(hs_cdscode) grades(6) title(Student Attrition in Future Grades) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/dyngrade_attrition.png)


********************
*** Peer Effects ***
********************
use ../data/lausd_data/analysis/student_year_main, clear
set matsize 10000
xtset studentid year

renvars ztva_cog_factor ztva_jackson_factor ztva_noncog_factor, prefix(k)
drop zt* j_z*	
renvars kztva_cog_factor kztva_jackson_factor kztva_noncog_factor, presub(k )

foreach var in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor{
	forvalues x = 2/3{
		gen `var'`x'=`var'^`x'
	}
}

*gen cool = uniform()
*keep if cool < .1

*** Create one year lagged leave out own value class average 
sort studentid year

foreach var in cog_factor jackson_factor noncog_factor {
	gen tempvar = L.`var'
	egen total`var' = total(tempvar), by(classid)	
	egen count`var' = count(tempvar), by(classid)
	gen locm_l`var' = (total`var' - tempvar)/(count`var' - 1)
	drop tempvar total`var'
}	
	
/*
foreach var in zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr logdaysabsent suspended heldback {
	gen tempvar = L.`var'
	egen total`var' = total(tempvar), by(classid)	
	egen count`var' = count(tempvar), by(classid)
	gen locm_l`var' = (total`var' - tempvar)/(count`var' - 1)
	drop tempvar total`var'
}		
*/

preserve

sort classid 
drop if classid == classid[_n-1]

sum locm_lcog_factor if countcog_factor >= 10 & countcog_factor <= 50 
global locm_sd_cog_factor r(sd)
global locm_mean_cog_factor r(mean)
di $locm_sd_cog_factor
di $locm_mean_cog_factor

sum locm_ljackson_factor if countjackson_factor >= 10 & countjackson_factor <= 50
global locm_sd_jackson_factor r(sd)
global locm_mean_jackson_factor r(mean)
di $locm_sd_jackson_factor 
di $locm_mean_jackson_factor 

sum locm_lnoncog_factor if countnoncog_factor >= 10 & countnoncog_factor <= 50
global locm_sd_noncog_factor r(sd)
global locm_mean_noncog_factor r(mean)
di $locm_sd_noncog_factor
di $locm_mean_noncog_factor

restore 	


gen zlocm_lcog_factor = (locm_lcog_factor - $locm_mean_cog_factor)/$locm_sd_cog_factor
gen zlocm_ljackson_factor = (locm_ljackson_factor - $locm_mean_jackson_factor)/$locm_sd_jackson_factor
gen zlocm_lnoncog_factor = (locm_lnoncog_factor - $locm_mean_noncog_factor)/$locm_sd_noncog_factor
	
	
*** Create two year lagged leave out own value class average 
sort studentid year

foreach var in cog_factor jackson_factor noncog_factor {
	gen tempvar = L2.`var'
	egen l2total`var' = total(tempvar), by(classid)	
	egen l2count`var' = count(tempvar), by(classid)
	gen locm_l2`var' = (l2total`var' - tempvar)/(l2count`var' - 1)
	drop tempvar l2total`var'
}	

/*
foreach var in zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr logdaysabsent suspended heldback {
	gen tempvar = L2.`var'
	egen l2total`var' = total(tempvar), by(classid)	
	egen l2count`var' = count(tempvar), by(classid)
	gen locm_l2`var' = (l2total`var' - tempvar)/(l2count`var' - 1)
	drop tempvar l2total`var'
}		
*/

preserve

sort classid 
drop if classid == classid[_n-1]

sum locm_l2cog_factor if l2countcog_factor >= 10 & l2countcog_factor <= 50 
global l2locm_sd_cog_factor r(sd)
global l2locm_mean_cog_factor r(mean)
di $l2locm_sd_cog_factor
di $l2locm_mean_cog_factor

sum locm_l2jackson_factor if l2countjackson_factor >= 10 & l2countjackson_factor <= 50
global l2locm_sd_jackson_factor r(sd)
global l2locm_mean_jackson_factor r(mean)
di $l2locm_sd_jackson_factor 
di $l2locm_mean_jackson_factor 

sum locm_l2noncog_factor if l2countnoncog_factor >= 10 & l2countnoncog_factor <= 50
global l2locm_sd_noncog_factor r(sd)
global l2locm_mean_noncog_factor r(mean)
di $l2locm_sd_noncog_factor
di $l2locm_mean_noncog_factor

restore 	


gen zlocm_l2cog_factor = (locm_l2cog_factor - $l2locm_mean_cog_factor)/$l2locm_sd_cog_factor
gen zlocm_l2jackson_factor = (locm_l2jackson_factor - $l2locm_mean_jackson_factor)/$l2locm_sd_jackson_factor
gen zlocm_l2noncog_factor = (locm_l2noncog_factor - $l2locm_mean_noncog_factor)/$l2locm_sd_noncog_factor
	
	
**** Create lagged variables since a2reg doesn't allow timeseries operators (e.g., L.)
foreach var in 	zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr logdaysabsent suspended heldback {
	gen l_`var' = L.`var'
}

foreach var in 	zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr {
	gen l_`var'2 = l_`var'^2
	gen l_`var'3 = l_`var'^3
	
}
		

**** Create two lagged variables since a2reg doesn't allow timeseries operators (e.g., L.)
foreach var in 	zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr logdaysabsent suspended heldback {
	gen l2_`var' = L2.`var'
}

foreach var in 	zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr {
	gen l2_`var'2 = l2_`var'^2
	gen l2_`var'3 = l2_`var'^3
}

gen l_ell = L.ell
gen l_grade = L.grade
gen l_year = L.year


***************************************************
***** Program to plot the dynamic peer effect *****
***************************************************
**** Do student fixed effects models. (or do student and teacher fixed effects models).  

cap program drop peerplot
program peerplot
	syntax [anything] [if] , depvar(string) plotvar(string) plotvarlag(string) controls_lead(string) febig(string) fesmall(string) controls_lag(string) cluster(string) /// 
	factornames(string) leads(string) title(string) ytitle(string) xtitle(string) output(string)
	cap matrix drop MSAVE
	local nplot=wordcount("`plotvar'")
	local x = `nplot'*3
	local y = `leads'+2
	
	local counter =1
	foreach var in `plotvar'{
		local pvar`counter' = "`var'"
		local ++counter
	}
	
	local counter =1
	foreach var in `plotvarlag'{
		local pvarlag`counter' = "`var'"
		local ++counter
	}
	
	local counter =1
	foreach var in `factornames'{
		local fvar`counter' = "`var'"
		local ++counter
	}
	
	*** These two lines of code are needed just if the first variable has two words in it instead of one
	local fvar1 = "`fvar1' `fvar2'"
	local fvar2 = "`fvar3'"
	local fvar3 = "`fvar4'"
	
	di "`pvar1'"
	
	matrix MSAVE = J(`y',`x',.)
	local leadcounter = 1
		areg L.`depvar' `plotvarlag' `controls_lag' `if', cluster(`cluster') a(`febig')
			matrix MSAVE[`leadcounter',1] = _b[`pvarlag1'] 
			matrix MSAVE[`leadcounter',2] = _b[`pvarlag1']  - invttail(e(df_r),.025)*_se[`pvarlag1']
			matrix MSAVE[`leadcounter',3] = _b[`pvarlag1']  + invttail(e(df_r),.025)*_se[`pvarlag1']
			if `nplot'>=2{
				matrix MSAVE[`leadcounter',4] = _b[`pvarlag2'] 
				matrix MSAVE[`leadcounter',5] = _b[`pvarlag2']  - invttail(e(df_r),.025)*_se[`pvarlag2']
				matrix MSAVE[`leadcounter',6] = _b[`pvarlag2']  + invttail(e(df_r),.025)*_se[`pvarlag2']
			}
			if `nplot'>=3{
				matrix MSAVE[`leadcounter',7] = _b[`pvarlag3'] 
				matrix MSAVE[`leadcounter',8] = _b[`pvarlag3']  - invttail(e(df_r),.025)*_se[`pvarlag3']
				matrix MSAVE[`leadcounter',9] = _b[`pvarlag3']  + invttail(e(df_r),.025)*_se[`pvarlag3']
			}
			local ++leadcounter
		
	forvalues lead = 0/`leads'{
		areg F(`lead').`depvar' `plotvar' `controls_lead' `if', cluster(`cluster') a(`febig')
			matrix MSAVE[`leadcounter',1] = _b[`pvar1'] 
			matrix MSAVE[`leadcounter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`leadcounter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`leadcounter',4] = _b[`pvar2'] 
				matrix MSAVE[`leadcounter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`leadcounter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`leadcounter',7] = _b[`pvar3'] 
				matrix MSAVE[`leadcounter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`leadcounter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++leadcounter
		}
	preserve
	clear
	svmat MSAVE
	gen relyear = _n-2
	drop if relyear == -1
	
	gen relyear2 = relyear - 0.03
	gen relyear3 = relyear + 0.03
		
	if `nplot'==1{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black) ///
			yline(0, lcolor(gs8)) xlabel(0(1)`leads') ylabel(,nogrid) ///
			legend(off) graphregion(color(white)) lcolor(black) title(`title') ytitle(`ytitle') xtitle(`xtitle'))
	}
	if `nplot'==2{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black) ///
			yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			lcolor(black) graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(0(1)`leads') ///
			legend(order(1 2) label(1 "`fvar1'") label(2 "`fvar2'")))

	}
	if `nplot'==3{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(dot)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(connected MSAVE7 relyear3, mcolor(black) msize(small) lcolor(black) msymbol(triangle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black)) ///
			(rcap  MSAVE8 MSAVE9 relyear3, lcolor(black) yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(0(1)`leads') ///
			legend(order(1 2 3) label(1 "`fvar1'") label(2 "`fvar2'") label(3 "`fvar3'") cols(3)))
	}
	graph export `output', replace
		
	restore
end

#delimit ;
global controls "l_zcstmath l_zcstela l_gpatotal l_effmarktotalyr l_habmarkfirst5totalyr l_habmarklast9totalyr l_zcstmath? 
	l_zcstela? l_gpatotal? l_effmarktotalyr? l_habmarkfirst5totalyr? l_habmarklast9totalyr? l_logdaysabsent l_suspended l_heldback
	ell i.grade i.year";

global l2controls "l2_zcstmath l2_zcstela l2_gpatotal l2_effmarktotalyr l2_habmarkfirst5totalyr l2_habmarklast9totalyr l2_zcstmath? 
	l2_zcstela? l2_gpatotal? l2_effmarktotalyr? l2_habmarkfirst5totalyr? l2_habmarklast9totalyr? l2_logdaysabsent l2_suspended l2_heldback
	l_ell i.l_grade i.l_year";

local controls_import "zcstmath zcstela gpatotal effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? habmarkfirst5totalyr? habmarklast9totalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr grade_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Lhabmarklast9totalyr? grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr class_Lhabmarkfirst5totalyr class_Lhabmarklast9totalyr
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? class_Lhabmarkfirst5totalyr? 
	class_Lhabmarklast9totalyr? class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell";
	
#delimit cr

***************************************
***** dynamic peer effect Figures *****
***************************************
egen schoolid = group(cdscode)

peerplot if grade >= 3 & grade <= 5, depvar(zcstmath) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(3) title(Math Test Scores) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_zcstmath.png)
	
peerplot if grade >= 3 & grade <= 5, depvar(zcstela) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(3) title(English Test Scores) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_zcstela.png)

peerplot if grade >= 3 & grade <= 5, depvar(gpatotal) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(4) title(GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_gpatotal.png)
	
peerplot if grade >= 3 & grade <= 5, depvar(effmarktotalyr) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(2) title(Effort GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_effmarktotalyr.png)
	
peerplot if grade >= 3 & grade <= 5, depvar(habmarktotalyr) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(2) title(Habit GPA) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_habmarktotalyr.png)
	
peerplot if grade >= 3 & grade <= 5, depvar(fracdaysabsent) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(4) title(Fraction of Days Absent) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_fracdaysabsent.png)		

peerplot if grade >= 3 & grade <= 5, depvar(heldback) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(3) title(Held Back a Grade) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_heldback.png)	
	
peerplot if grade >= 3 & grade <= 5, depvar(logdaysabsent) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(4) title(Log Days Absent) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_logdaysabsent.png)

peerplot if grade >= 3 & grade <= 5, depvar(suspended) plotvar(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor) ///
	plotvarlag(zlocm_l2cog_factor zlocm_l2jackson_factor zlocm_l2noncog_factor) febig(studentid) fesmall(cdscode) ///
	factornames(Test Score Behavior Learning) ///
	controls_lead($controls) controls_lag($l2controls) ///
	cluster(hs_cdscode) leads(3) title(Suspended) ytitle(Coefficient Estimate) xtitle(Relative Year) ///
	output(../output/peereffect_suspended.png)


********************************************************************************
*** TABLE 30: Peer Effects on High School Outcomes using 3 Factor Variables ***
********************************************************************************
cap file close tables 
file open tables using "../output/table30_peersmall.txt", write replace
file write tables "<tab:peersmall>" _n
cap file close tables 


*** reg2hdfe
*** a2reg
*** felsdvreg
*** felsdvregdm
*** mi_twoway
*** twfe

egen schoolgradeid = group(cdscode grade)

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	areg `var' zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor ///
		$controls if grade >= 3 & grade <= 5, cluster(hs_cdscode) a(schoolgradeid)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table30_peersmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor)
/*
********************************************************************************
*** TABLE 30b: Peer Effects on High School Outcomes using all Factor Variables ***
********************************************************************************
cap file close tables 
file open tables using "../output/table30b_peerbig.txt", write replace
file write tables "<tab:peerbig>" _n
cap file close tables 

*egen schoolgradeid = group(cdscode grade)

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	areg `var' zlocm_zcstmath zlocm_zcstela zlocm_gpatotal zlocm_effmarktotalyr zlocm_habmarkfirst5totalyr ///
		zlocm_habmarklast9totalyr zlocm_logdaysabsent zlocm_suspended zlocm_heldback ///
		$controls if grade >= 3 & grade <= 5, cluster(hs_cdscode) a(schoolgradeid)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table30b_peerbig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor)

*/
********************************************************************************
*** TABLE 31: Peer Effects on same year outcomes using 3 Factor Variables ***
********************************************************************************
cap file close tables 
file open tables using "../output/table31_peersameyrsmall.txt", write replace
file write tables "<tab:peersameyrsmall>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in gpatotal effmarktotalyr habmarktotalyr zcstmath zcstela dayssuspended logdaysabsent heldback { 
	areg `var' zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor ///
		$controls if grade >= 3 & grade <= 5, cluster(hs_cdscode) a(studentid)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table31_peersameyrsmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor)
	
	
/*	
********************************************************************************
*** TABLE 31b: Peer Effects on same year outcomes using All Variables ***
********************************************************************************
cap file close tables 
file open tables using "../output/table31b_peersameyrbig.txt", write replace
file write tables "<tab:peersameyrbig>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in gpatotal effmarktotalyr habmarktotalyr zcstmath zcstela fracdaysabsent heldback logdaysabsent suspended { 
	areg `var' zlocm_zcstmath zlocm_zcstela zlocm_gpatotal zlocm_effmarktotalyr zlocm_habmarkfirst5totalyr ///
		zlocm_habmarklast9totalyr zlocm_logdaysabsent zlocm_suspended zlocm_heldback ///
		$controls if grade >= 3 & grade <= 5, cluster(hs_cdscode) a(studentid)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table31b_peersameyrbig.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor)
*/	

**************************************************************************************************
*** TABLE 31c: Peer Effects on same year outcomes using 3 Factor Variables Controlling for TVA ***
**************************************************************************************************
cap file close tables 
file open tables using "../output/table31c_peersameyrsmall.txt", write replace
file write tables "<tab:peersameyrsmallc>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in gpatotal effmarktotalyr habmarktotalyr zcstmath zcstela dayssuspended logdaysabsent heldback { 
	areg `var' zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor ///
		 ztva_cog_factor ztva_cog_factor? ztva_jackson_factor ztva_jackson_factor? ///
		 ztva_noncog_factor ztva_noncog_factor? $controls if grade >= 3 & grade <= 5, ///
		 cluster(hs_cdscode) a(studentid)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table31c_peersameyrsmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(zlocm_lcog_factor zlocm_ljackson_factor zlocm_lnoncog_factor)
*/

*****************************************
*** Plot Effects on Outcomes by Grade ***
*****************************************
use ../data/lausd_data/analysis/student_year_main, clear
drop j_z* tva* ztvaqe* r_*  oth_* c_* r_* b_ztva* f_ztva*

drop grade_Lhabmarkfirst5totalyr class_Lhabmarkfirst5totalyr grade_Lhabmarklast9totalyr /// 
	class_Lhabmarklast9totalyr grade_Lhabmarkfirst5totalep3 class_Lhabmarkfirst5totalep3 /// 
	grade_Lhabmarklast9totalep3 class_Lhabmarklast9totalep3  /// 
	 habmarkfirst5totalyr2 grade_Lhabmarkfirst5totalyr2 class_Lhabmarkfirst5totalyr2 /// 
	habmarkfirst5totalyr3 grade_Lhabmarkfirst5totalyr3 class_Lhabmarkfirst5totalyr3 /// 
	habmarklast9totalyr2 grade_Lhabmarklast9totalyr2 class_Lhabmarklast9totalyr2 /// 
	habmarklast9totalyr3 grade_Lhabmarklast9totalyr3 class_Lhabmarklast9totalyr3 ///
	habmarkfirst5totalep32-class_Lhabmarklast9totalep33 habmarktotalep1-classid ///
	
	
	
xtset studentid year

fvset base 2007 year
fvset base 3 grade

cap program drop plotgrades
program plotgrades
	syntax [anything] , depvar(string) plotvar(string) controls(string) grades(string) cluster(string) /// 
		factornames(string) title(string) ytitle(string) xtitle(string) output(string) boxpos(string) ///
		[secondary_plotvar(string) secondary_controls(string)]
	cap matrix drop MSAVE
	local nplot=wordcount("`plotvar'")
	local counter=1
	forvalues grade = `grades'{
		if `counter'==1{
			local firstgrade = `grade'
		}
		local g=`grade'
		local ++counter
	}
	
	local x = `nplot'*3
	local y = `g'-`first_grade'+1
	
	local counter =1
	foreach var in `plotvar'{
		local pvar`counter' = "`var'"
		local ++counter
	}
	local counter =1
	foreach var in `factornames'{
		local fvar`counter' = "`var'"
		local ++counter
	}
	
	*** These two lines of code are needed just if the first variable has two words in it instead of one
	local fvar1 = "`fvar1' `fvar2'"
	local fvar2 = "`fvar3'"
	
	if "`secondary_plotvar'"==""{
		local secondary_plotvar "`plotvar'"
	}
	if "`secondary_controls'"==""{
		local secondary_controls "`controls'"
	}
		
	matrix MSAVE = J(`y',`x',.)
	local counter = 1
	forvalues grade = `grades'{
	di "`grade'"
	  if `grade'<6{
		reg `depvar' `plotvar' `controls' if grade==`grade', cluster(`cluster') 
	  }
	  if `grade'>=6{
	  	reg `depvar' `secondary_plotvar' `secondary_controls' if grade==`grade', cluster(`cluster') 
	  }
			matrix MSAVE[`counter',1] = _b[`pvar1'] 
			matrix MSAVE[`counter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`counter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			if `nplot'>=2{
				matrix MSAVE[`counter',4] = _b[`pvar2'] 
				matrix MSAVE[`counter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
				matrix MSAVE[`counter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
			}
			if `nplot'>=3{
				matrix MSAVE[`counter',7] = _b[`pvar3'] 
				matrix MSAVE[`counter',8] = _b[`pvar3']  - invttail(e(df_r),.025)*_se[`pvar3']
				matrix MSAVE[`counter',9] = _b[`pvar3']  + invttail(e(df_r),.025)*_se[`pvar3']
			}
			local ++counter
		}
	preserve
	clear
	svmat MSAVE
	gen relyear = _n+`firstgrade'-1
	drop if relyear >= 13
	drop if MSAVE1 == . 
	gen relyear2 = relyear - 0.04
	gen relyear3 = relyear + 0.04
	
	egen totalmsave1 = total(MSAVE1) 
	sum totalmsave1 
	local sum1: display %5.3fc r(mean)
	if "`depvar'" == "satscoretotal" {
		local sum1: display %5.0fc r(mean)
	}

	cap egen totalmsave2 = total(MSAVE4) 
	cap sum totalmsave2 
	cap local sum2: display %5.3fc r(mean)
	if "`depvar'" == "satscoretotal" {
		local sum2: display %5.0fc r(mean)
	}
	
	cap egen totalmsave3 = total(MSAVE7) 
	cap sum totalmsave3
	cap local sum3: display %5.3fc r(mean)
	if "`depvar'" == "satscoretotal" {
		local sum3: display %5.0fc r(mean)
	}
	
	if `nplot'==1{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black) ///
			yline(0, lcolor(gs8)) xlabel(0(1)`leads') ylabel(,nogrid) ///
			legend(off) graphregion(color(white)) lcolor(black) title(`title') ytitle(`ytitle') xtitle(`xtitle') ///
			note("`fvar1' Sum = `sum1'", box fcolor(none) placement(`boxpos') ring(0) justification(right) size(large)))
	}
	if `nplot'==2{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black) ///
			yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			lcolor(black) graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(`grades') ///
			legend(order(1 2) label(1 "`fvar1'") label(2 "`fvar2'")) ///
			note("`fvar1' Sum = `sum1'" "`fvar2' Sum = `sum2'", box fcolor(none) placement(`boxpos') ring(0) justification(right) size(large)))

	}
	if `nplot'==3{
		twoway (connected MSAVE1 relyear, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(dot)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(connected MSAVE7 relyear3, mcolor(black) msize(small) lcolor(black) msymbol(triangle) lpattern(solid)) ///
			(rcap  MSAVE2 MSAVE3 relyear, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black)) ///
			(rcap  MSAVE8 MSAVE9 relyear3, lcolor(black) yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(`grades') ///
			legend(order(1 2 3) label(1 "`fvar1'") label(2 "`fvar2'") label(3 "`fvar3'") cols(3)) ///
			note("`fvar1' Sum = `sum1'" "`fvar2' Sum = `sum2'" "`fvar3' Sum = `sum3'", box fcolor(none) placement(`boxpos') ring(0) justification(right) size(large)))
	}
	graph export `output', replace
		
	restore
end


cap program drop plotgrades_tracking
program plotgrades_tracking
	syntax [anything] , depvar(string) plotvar(string) controls(string) grades(string) cluster(string) /// 
		factornames(string) title(string) ytitle(string) xtitle(string) output(string) boxpos(string)
	cap matrix drop MSAVE
	local nplot=wordcount("`plotvar'")
	local counter=1
	forvalues grade = `grades'{
		if `counter'==1{
			local firstgrade = `grade'
		}
		local g=`grade'
		local ++counter
	}
	
	local x = `nplot'*3+4
	local y = `g'-`first_grade'+1
	
	local counter =1
	foreach var in `plotvar'{
		local pvar`counter' = "`var'"
		local ++counter
	}
	local counter =1
	foreach var in `factornames'{
		local fvar`counter' = "`var'"
		local ++counter
	}
	
	*** These two lines of code are needed just if the first variable has two words in it instead of one
	local fvar1 = "`fvar1' `fvar2'"
	local fvar2 = "`fvar3'"
	
	matrix MSAVE = J(`y',`x',.)
	local counter = 1
	forvalues grade = `grades'{
	di "`grade'"
		reg `depvar' `plotvar' `controls' if grade==`grade', cluster(`cluster') 
			matrix MSAVE[`counter',1] = _b[`pvar1'] 
			matrix MSAVE[`counter',2] = _b[`pvar1']  - invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`counter',3] = _b[`pvar1']  + invttail(e(df_r),.025)*_se[`pvar1']
			matrix MSAVE[`counter',4] = _b[`pvar2'] 
			matrix MSAVE[`counter',5] = _b[`pvar2']  - invttail(e(df_r),.025)*_se[`pvar2']
			matrix MSAVE[`counter',6] = _b[`pvar2']  + invttail(e(df_r),.025)*_se[`pvar2']
		 reg F.`pvar1' `plotvar' `controls' if grade==`grade', cluster(`cluster') 
			matrix MSAVE[`counter',7] = _b[`pvar1'] 
			matrix MSAVE[`counter',8] = _b[`pvar2'] 
		 reg F.`pvar2' `plotvar' `controls' if grade==`grade', cluster(`cluster') 
			matrix MSAVE[`counter',9] =  _b[`pvar1'] 
			matrix MSAVE[`counter',10] = _b[`pvar2'] 
			local ++counter
	}

	preserve
	clear
	svmat MSAVE
	drop if MSAVE1 == . 
	* Estimates Accounting for Tracking
	gen grade=_n+`firstgrade'-1
	gsort -grade
	gen kappacog=0
	gen kappabehave=0
	local N=_N
	
	forvalues ob=1/`N'{
		* Assume estimate grade 12 effect is equal to true grade 12 effect
		if `ob'==1{
			replace kappacog=MSAVE1 if _n==`ob'
			replace kappabehave=MSAVE4 if _n==`ob'
		}
		* Assume estimate grade 11 to grade 3 effects recursively assuming
		* an additive education production function and observed tracking coefficients
		* equal actual tracking coefficients.
		else{
			scalar kappacog_tracksum=0
			scalar kappabehave_tracksum=0
			forvalues x=2/`ob'{
				scalar kappacog_tracksum=scalar(kappacog_tracksum)+ ///
					MSAVE7[`x']*kappacog[(`x'-1)]+MSAVE8[`x']*kappacog[(`x'-1)]
			
				scalar kappabehave_tracksum=scalar(kappabehave_tracksum)+ ///
					MSAVE9[`x']*kappabehave[(`x'-1)]+MSAVE10[`x']*kappabehave[(`x'-1)]
					
			}
			di kappacog_tracksum
			di kappabehave_tracksum
			replace kappacog=MSAVE1-scalar(kappacog_tracksum) if _n==`ob'
			replace kappabehave=MSAVE4-scalar(kappabehave_tracksum) if _n==`ob'
					
		}
	}		
    sort grade
	
	gen relyear = _n+`firstgrade'-1
	drop if relyear >= 13

	gen relyear2 = relyear - 0.04
	gen relyear3 = relyear + 0.04
	
	egen totalmsave1 = total(MSAVE1) 
	sum totalmsave1 
	local sum1: display %5.3fc r(mean)
	if "`depvar'" == "satscoretotal" {
		local sum1: display %5.0fc r(mean)
	}

	cap egen totalmsave2 = total(MSAVE4) 
	cap sum totalmsave2 
	cap local sum2: display %5.3fc r(mean)
	if "`depvar'" == "satscoretotal" {
		local sum2: display %5.0fc r(mean)
	}
	
	cap egen totalmsave3 = total(MSAVE7) 
	cap sum totalmsave3
	cap local sum3: display %5.3fc r(mean)
	if "`depvar'" == "satscoretotal" {
		local sum3: display %5.0fc r(mean)
	}
	
	if `nplot'==2{
		twoway (connected MSAVE1 relyear3, mcolor(black) msize(small) lcolor(black) msymbol(circle) lpattern(solid)) ///
			(connected MSAVE4 relyear2, mcolor(black) msize(small) lcolor(black) msymbol(square) lpattern(dash)) ///
			(scatter kappacog relyear2, mcolor(black) msize(medium) msymbol(circle_hollow) ) ///
			(scatter kappabehave relyear3, mcolor(black) msize(medium) msymbol(square_hollow)) ///
			(rcap  MSAVE2 MSAVE3 relyear3, lcolor(black)) ///
			(rcap  MSAVE5 MSAVE6 relyear2, lcolor(black) ///
			yline(0, lcolor(gs8)) ylabel(,nogrid) ///
			lcolor(black) graphregion(color(white)) title(`title') ytitle(`ytitle') xtitle(`xtitle') xlabel(`grades') ///
			legend(order(1 2 3 4) label(1 "`fvar1'") label(2 "`fvar2'") label(3 "`fvar1' - Tracking") label(4 "`fvar2' - Tracking")) ///
			note("`fvar1' Sum = `sum1'" "`fvar2' Sum = `sum2'", box fcolor(none) placement(`boxpos') ring(0) justification(right) size(large)))

	}

	graph export `output', replace
	save ./data.dta	, replace
	restore
end

#delimit ;

local controls_secondary "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.zcstmath? 
	L.zcstela? L.gpatotal? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";
	
local ctrls_ftrack "i.grade#C.(L.zcstmath L.zcstela L.gpatotal  L.zcstmath? 
	L.zcstela? L.gpatotal? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?  
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year i.hs_trackmath_a i.hs_trackmath_b i.hs_trackmath_c i.hs_trackmath_r
	i.hs_apmath_classes i.hs_apenglish_classes i.hs_ela_elective i.hs_ela_elective";

local controls_secondary_track "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.zcstmath? 
	L.zcstela? L.gpatotal? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal?  
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal?
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year i.trackmath_a i.trackmath_b i.trackmath_c i.trackmath_r
	i.apmath_classes i.apenglish_classes i.ela_elective i.ela_elective";
#delimit cr	

/*
* VA and controls adjusted for tracking
plotgrades , depvar(hs_dropout) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') ///
	grades(3(1)12) boxpos(1) ///
	cluster(hs_cdscode) title(HS Dropout) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_hs_dropout.pdf)

plotgrades , depvar(evergrad) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(HS Graduate) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_evergrad.pdf)
		
plotgrades , depvar(eversat) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(Took SAT) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_eversat.pdf)
		
plotgrades , depvar(satscoretotal) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(1) ///
	cluster(hs_cdscode) title(SAT Score) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_satscoretotal.pdf)
		
plotgrades , depvar(gpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_gpatotal12.pdf)	

plotgrades , depvar(workeffortgpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade Effort GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_workeffortgpatotal12.pdf)
				
plotgrades , depvar(coopgpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade Cooperation GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_coopgpatotal12.pdf)
		
plotgrades , depvar(zcstmath11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)11) boxpos(5) ///
	cluster(hs_cdscode) title(11th Grade Math Test Scores) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_zcstmath11.pdf)
	
plotgrades , depvar(zcstela11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)11) boxpos(5) ///
	cluster(hs_cdscode) title(11th Grade English Test Scores) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_zcstela11.pdf)

plotgrades , depvar(dayssuspended12 ) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(7) ///
	cluster(hs_cdscode) title(12th Grade Days Suspended) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_dayssuspended12.pdf)
	
plotgrades , depvar(logdaysabsent12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)12) boxpos(7) ///
	cluster(hs_cdscode) title(12th Grade Log Days Absent) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_logdaysabsent12.pdf)

plotgrades , depvar(heldback11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`ctrls_ftrack') ///
	secondary_plotvar(ztva_cog_factor_track ztva_jackson_factor_track) ///
	secondary_controls(`controls_secondary_track') /// 
	grades(3(1)11) boxpos(7) ///
	cluster(hs_cdscode) title(11th Grade Held Back) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_trackctrl_heldback12.pdf)
	



* Removing Tracking
plotgrades_tracking , depvar(hs_dropout) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(1) ///
	cluster(hs_cdscode) title(HS Dropout) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_hs_dropout.pdf)

plotgrades_tracking , depvar(evergrad) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(HS Graduate) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_evergrad.pdf)
		
plotgrades_tracking , depvar(eversat) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(Took SAT) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_eversat.pdf)
		
plotgrades_tracking , depvar(satscoretotal) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(1) ///
	cluster(hs_cdscode) title(SAT Score) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_satscoretotal.pdf)
		
plotgrades_tracking , depvar(gpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_gpatotal12.pdf)	

plotgrades_tracking , depvar(workeffortgpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade Effort GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_workeffortgpatotal12.pdf)
				
plotgrades_tracking , depvar(coopgpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade Cooperation GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_coopgpatotal12.pdf)
		
plotgrades_tracking , depvar(zcstmath11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)11) boxpos(5) ///
	cluster(hs_cdscode) title(11th Grade Math Test Scores) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_zcstmath11.pdf)
	
plotgrades_tracking , depvar(zcstela11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)11) boxpos(5) ///
	cluster(hs_cdscode) title(11th Grade English Test Scores) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_zcstela11.pdf)

plotgrades_tracking , depvar(dayssuspended12 ) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(7) ///
	cluster(hs_cdscode) title(12th Grade Days Suspended) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_dayssuspended12.pdf)
	
plotgrades_tracking , depvar(logdaysabsent12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(7) ///
	cluster(hs_cdscode) title(12th Grade Log Days Absent) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_logdaysabsent12.pdf)

plotgrades_tracking , depvar(heldback11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)11) boxpos(7) ///
	cluster(hs_cdscode) title(11th Grade Held Back) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_track_heldback12.pdf)
	
*/


* Original version
/*
plotgrades , depvar(hs_zalltest) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(SAT + CAHSEE Test Score Index) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_idxhs_zalltest.eps)

plotgrades , depvar(hs_zcahsee) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(CAHSEE Test Score Index) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_idxhs_zcahsee.eps)

plotgrades , depvar(hs_znontest) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(Non-Test Score Index) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_idxhs_znontest.eps)
 
plotgrades , depvar(hs_zbehave) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(Behavior Index) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_idxhs_zbehave.eps)

plotgrades , depvar(hs_zbehavlong) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(Behavior Index) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_idxhs_zbehavlong.eps)

plotgrades , depvar(hs_zgpa) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(GPA Index) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_idxhs_zgpa.eps)
	
plotgrades , depvar(apclasstotal) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(AP Courses) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_apclasstotal.eps)
*/
plotgrades , depvar(hs_dropout) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(1) ///
	cluster(hs_cdscode) title(HS Dropout) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_hs_dropout.eps)

plotgrades , depvar(evergrad) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(5) ///
	cluster(hs_cdscode) title(HS Graduate) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_evergrad.eps)
		
plotgrades , depvar(eversat) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(Took SAT) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_eversat.eps)
		
plotgrades , depvar(satscoretotal) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(1) ///
	cluster(hs_cdscode) title(SAT Score) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_satscoretotal.eps)
		
plotgrades , depvar(gpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_gpatotal12.eps)	

plotgrades , depvar(workeffortgpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade Effort GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_workeffortgpatotal12.eps)
				
plotgrades , depvar(coopgpatotal12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(11) ///
	cluster(hs_cdscode) title(12th Grade Cooperation GPA) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_coopgpatotal12.eps)
		
plotgrades , depvar(zcstmath11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)11) boxpos(5) ///
	cluster(hs_cdscode) title(11th Grade Math Test Scores) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_zcstmath11.eps)
	
plotgrades , depvar(zcstela11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)11) boxpos(5) ///
	cluster(hs_cdscode) title(11th Grade English Test Scores) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_zcstela11.eps)

plotgrades , depvar(dayssuspended12 ) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(7) ///
	cluster(hs_cdscode) title(12th Grade Days Suspended) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_dayssuspended12.eps)
	
plotgrades , depvar(logdaysabsent12) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)12) boxpos(7) ///
	cluster(hs_cdscode) title(12th Grade Log Days Absent) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_logdaysabsent12.eps)

plotgrades , depvar(heldback11) plotvar(ztva_cog_factor ztva_jackson_factor) ///
	factornames(Test Score Behavior) ///
	controls(`controls_secondary') ///
	grades(3(1)11) boxpos(7) ///
	cluster(hs_cdscode) title(11th Grade Held Back) ytitle(Coefficient Estimate) xtitle(Grade) ///
	output(../output/gradefactor_heldback12.eps)
	

*****************************************************
*** Two dimensional Factor Densities/Correlations ***
*****************************************************
preserve
use ../data/lausd_data/analysis/student_year_main, clear

corr ztva_cog_factor ztva_jackson_factor ztva_noncog_factor if grade >= 3 & grade <= 5
matrix corrcoef = r(C)

local corr_cog_jack: display %5.3fc corrcoef[2,1]
local corr_cog_noncog: display %5.3fc corrcoef[3,1]
local corr_jack_noncog: display %5.3fc corrcoef[3,2]

restore 

preserve
use ../data/lausd_data/vascores/biastest_studentlevel, clear

keep if grade >= 2 & grade <= 5 
drop if teacherid == . 

*** Make teacher-year level dataset 
egen studentcount = count(year), by(teacherid year)
drop if studentcount < 10 | studentcount > 35

collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarktotalyr {
		egen fn_`var' = std(score_r_`var')
}

gen ztva_cog_factor = fn_zcstmath + fn_zcstela
gen ztva_jackson_factor = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback
gen ztva_noncog_factor = fn_effmarktotalyr + fn_habmarktotalyr

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

gen cogbyjack = 0.5*ztva_cog_factor + 0.5*ztva_jackson_factor
gen cogbynoncog = 0.5*ztva_cog_factor + 0.5*ztva_noncog_factor
gen jackbynoncog = 0.5*ztva_jackson_factor + 0.5*ztva_noncog_factor

**********************************************************************
*** Teacher by Year Two Dimensional Factor Densities/Correlations  ***
**********************************************************************
sum ztva_cog_factor, detail
local cog5p = r(p5)
local cog1p = r(p1)
local cog99p = r(p99) 
local cog95p = r(p95) 

sum ztva_jackson_factor, detail
local jack5p = r(p5)
local jack1p = r(p1)
local jack99p = r(p99) 
local jack95p = r(p95) 

sum ztva_noncog_factor, detail
local noncog5p = r(p5)
local noncog1p = r(p1)
local noncog99p = r(p99) 
local noncog95p = r(p95) 

sum cogbyjack, detail 
local cogbyjack5p = r(p5)

sum cogbynoncog, detail 
local cogbynoncog5p = r(p5)

sum jackbynoncog, detail 
local jackbynoncog5p = r(p5)

label var ztva_cog_factor "Test Score Value-Added"
label var ztva_jackson_factor "Behavior Value-Added"
label var ztva_noncog_factor "Learning Skills Value-Added"

local range_cogbyjack = (-`cogbyjack5p' + 0.5*(-3))/(-0.5)
di `range_cogbyjack'

local range_cogbynoncog = (-`cogbynoncog5p' + 0.5*(-3))/(-0.5)
di `range_cogbynoncog'

local range_jackbynoncog = (-`jackbynoncog5p' + 0.5*(-3))/(-0.5)
di `range_jackbynoncog'
/*
******** HAVE NO VERTICAL or HORIZANTAL 5 PERCENT LINES *************
*** Cognitive by Jackson
graph twoway (scatter ztva_cog_factor ztva_jackson_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_jackson_factor > -3 & ztva_jackson_factor < 3, msymbol(p) mcolor(gs5) ///
	ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Behavior) ///
	note("Corr = `corr_cog_jack'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_tchyr_cog_jack_nolines.png", replace    	


********* JUST HAVE HORIZANTAL 5 PERCENT LINES *************
*** Cognitive by Jackson
graph twoway (scatter ztva_cog_factor ztva_jackson_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_jackson_factor > -3 & ztva_jackson_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Behavior) ///
	note("Corr = `corr_cog_jack'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_tchyr_cog_jack_justhorizontallines.png", replace    	
*/

********* JUST HAVE VERTICAL AND HORIZANTAL 5 PERCENT LINES *************
*** Cognitive by Jackson
graph twoway (scatter ztva_cog_factor ztva_jackson_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_jackson_factor > -3 & ztva_jackson_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Behavior) ///
	note("Corr = `corr_cog_jack'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_tchyr_cog_jack.eps", replace    	

*** Cognitive by Noncognitive
graph twoway (scatter ztva_cog_factor ztva_noncog_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_noncog_factor > -3 & ztva_noncog_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Learning Skills) ///
	note("Corr = `corr_cog_noncog'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_tchyr_cog_noncog.eps", replace    	
 
*** Jackson by Noncognitive
graph twoway (scatter ztva_jackson_factor ztva_noncog_factor if ztva_jackson_factor > -3 & ztva_jackson_factor < 3 ///
	& ztva_noncog_factor > -3 & ztva_noncog_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Behavior by Learning Skills) ///
	note("Corr = `corr_jack_noncog'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_tchyr_jack_noncog.eps", replace    	

/*
********* ADD THE DIAGONAL LINE FOR 5% EQUAL LINEAR COMBINATION **************
*** Cognitive by Jackson
graph twoway (scatter ztva_cog_factor ztva_jackson_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_jackson_factor > -3 & ztva_jackson_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Behavior) xtitle(Behavior Value-Added) ytitle(Test Score Value-Added) ///
	note("Corr = `corr_cog_jack'", box fcolor(none) placement(5) ring(0) justification(right) size(large))) ///
	(function y = (-`cogbyjack5p' + 0.5*x)/(-0.5), range(-3 `range_cogbyjack') lcolor(gs4) lpattern(dash))
graph export "../output/twowaydensity_withdiag_tchyr_cog_jack.png", replace    	

*** Cognitive by Noncognitive
graph twoway (scatter ztva_cog_factor ztva_noncog_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_noncog_factor > -3 & ztva_noncog_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Learning Skills) xtitle(Learning Skills Value-Added) ytitle(Test Score Value-Added) ///
	note("Corr = `corr_cog_noncog'", box fcolor(none) placement(5) ring(0) justification(right) size(large))) ///
	(function y = (-`cogbyjack5p' + 0.5*x)/(-0.5), range(-3 `range_cogbynoncog') lcolor(gs4) lpattern(dash))
graph export "../output/twowaydensity_withdiag_tchyr_cog_noncog.png", replace    	
 
*** Jackson by Noncognitive
graph twoway (scatter ztva_jackson_factor ztva_noncog_factor if ztva_jackson_factor > -3 & ztva_jackson_factor < 3 ///
	& ztva_noncog_factor > -3 & ztva_noncog_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Behavior by Learning Skills) xtitle(Learning Skills Value-Added) ytitle(Behavior Value-Added) ///
	note("Corr = `corr_jack_noncog'", box fcolor(none) placement(5) ring(0) justification(right) size(large))) ///
	(function y = (-`cogbyjack5p' + 0.5*x)/(-0.5), range(-3 `range_jackbynoncog') lcolor(gs4) lpattern(dash))
graph export "../output/twowaydensity_withdiag_tchyr_jack_noncog.png", replace    	
*/
*******************************************************************
*** Just Teacher Two Dimensional Factor Densities/Correlations  ***
*******************************************************************
sort teacherid 
drop if teacherid == teacher[_n-1]

foreach var of varlist *factor* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}
sum ztva_cog_factor, detail
local cog5p = r(p5)
local cog1p = r(p1)
local cog99p = r(p99) 
local cog95p = r(p95) 

sum ztva_jackson_factor, detail
local jack5p = r(p5)
local jack1p = r(p1)
local jack99p = r(p99) 
local jack95p = r(p95) 

sum ztva_noncog_factor, detail
local noncog5p = r(p5)
local noncog1p = r(p1)
local noncog99p = r(p99) 
local noncog95p = r(p95) 

label var ztva_cog_factor "Test Score Value-Added"
label var ztva_jackson_factor "Behavior Value-Added"
label var ztva_noncog_factor "Learning Skills Value-Added"

*** Cognitive by Jackson
graph twoway (scatter ztva_cog_factor ztva_jackson_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_jackson_factor > -3 & ztva_jackson_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Behavior) ///
	note("Corr = `corr_cog_jack'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_justtch_cog_jack.png", replace    	

*** Cognitive by Noncognitive
graph twoway (scatter ztva_cog_factor ztva_noncog_factor if ztva_cog_factor > -3 & ztva_cog_factor < 3 ///
	& ztva_noncog_factor > -3 & ztva_noncog_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Test Score by Learning Skills) ///
	note("Corr = `corr_cog_noncog'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_justtch_cog_noncog.png", replace    	
 
*** Jackson by Noncognitive
graph twoway (scatter ztva_jackson_factor ztva_noncog_factor if ztva_jackson_factor > -3 & ztva_jackson_factor < 3 ///
	& ztva_noncog_factor > -3 & ztva_noncog_factor < 3, msymbol(p) mcolor(gs5) ///
	yline(`cog5p', lcolor(gs4) lpattern(dash)) xline(`jack5p', lcolor(gs4) lpattern(dash)) ylabel(-3(1)3, nogrid) ///
	xlabel(-3(1)3, nogrid) legend(off) graphregion(color(white)) ///
	title(Behavior by Learning Skills) ///
	note("Corr = `corr_jack_noncog'", box fcolor(none) placement(5) ring(0) justification(right) size(large)))
graph export "../output/twowaydensity_justtch_jack_noncog.png", replace    	

restore
		
*****************************************************
*** Figure: Elementary School VA Autocorrelations ***
*****************************************************
preserve 
use ../data/lausd_data/vascores/biastest_studentlevel, clear

*use year studentid grade teacherid cdscode ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
*	ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_logdaysabsent ztva_suspended ztva_heldback ///
*	ztva_effmarktotalyr ztva_habmarkfirst5totalyr ztva_habmarklast9totalyr using ../data/lausd_data/analysis/student_year_main, clear

keep if grade >= 2 & grade <= 5 
drop if teacherid == . 

*** Make teacher-year level dataset 
collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr {
		egen fn_`var' = std(score_r_`var')
}

gen score_r_cog_factor = fn_zcstmath + fn_zcstela
gen score_r_jackson_factor_even = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback
gen score_r_noncog_factor_even = fn_effmarktotalyr + fn_habmarkfirst5totalyr + fn_habmarklast9totalyr

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

*** Create lagged 
foreach var in score_r_cog_factor score_r_jackson_factor score_r_noncog_factor ///
	score_r_zcstmath score_r_zcstela score_r_habmarktotalyr score_r_gpatotal ///
	score_r_effmarktotalyr score_r_logdaysabsent score_r_suspended score_r_heldback {
		gen l1`var' = L1.`var'
		gen l2`var' = L2.`var'
		gen l3`var' = L3.`var'
		gen l4`var' = L4.`var'
		gen l5`var' = L5.`var'
		gen l6`var' = L6.`var'
		gen l7`var' = L7.`var'
}

*** Auto correlation matrix for the three factors
local lags = 6
matrix AUTO = J(`lags',3,.)

local column = 1 
foreach var in score_r_cog_factor score_r_jackson_factor score_r_noncog_factor {
*	score_r_zcstmath_math score_r_zcstela_english ///
*	score_r_gpatotal_math score_r_logdaysabsent_math score_r_suspended_math score_r_heldback_math /// 
*	score_r_gpatotal_english score_r_logdaysabsent_english score_r_suspended_english score_r_heldback_english
		di "*****"
		di "`var'" 
		di "*****"
		local row = 1
		forvalues x = 1/`lags' { 
			corr `var' l`x'`var'
			mat AUTO[`row',`column'] = r(rho)
			local ++row
		}
		local ++column
}

*** Auto correlation matrix for all the inputs
local lags = 6
matrix AUTObig = J(`lags',8,.)

local column = 1 
foreach var in score_r_zcstmath score_r_zcstela score_r_habmarktotalyr score_r_gpatotal ///
	score_r_effmarktotalyr score_r_logdaysabsent score_r_suspended score_r_heldback {
		di "*****"
		di "`var'" 
		di "*****"
		local row = 1
		forvalues x = 1/`lags' { 
			corr `var' l`x'`var'
			mat AUTObig[`row',`column'] = r(rho)
			local ++row
		}
		local ++column
}

clear
svmat AUTO
gen year = _n

graph twoway (connect AUTO1 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTO2 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)) ///
	(connect AUTO3 year, mcolor(black) lcolor(black) msymbol(triangle) lpattern(dot)), ///
	graphregion(color(white)) xlabel(0(1)6,nogrid) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2 3) lab(1 Test Score) lab(2 Behavior) lab(3 Learning) cols(3)) graphregion(color(white)) ///
	title(Elementary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_elem.png", replace

graph twoway (connect AUTO1 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTO2 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)) ///
	(connect AUTO3 year, mcolor(black) lcolor(black) msymbol(triangle) lpattern(dot)), ///
	graphregion(color(white)) xlabel(0(1)6,nogrid) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2 3) lab(1 Test Score) lab(2 Behavior) lab(3 Learning) cols(3)) graphregion(color(white)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_elem_forslides.png", replace



clear
svmat AUTObig
gen year = _n

graph twoway (connect AUTObig4 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTObig6 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)) ///
	(connect AUTObig7 year, mcolor(black) lcolor(black) msymbol(triangle) lpattern(dot)) ///
	(connect AUTObig8 year, mcolor(black) lcolor(black) msymbol(diamond) lpattern(dash_dot)), ///
	graphregion(color(white)) xlabel(0(1)6,nogrid) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2 3 4) lab(1 GPA) lab(2 Absences) lab(3 Suspended) lab(4 Heldback) cols(2)) graphregion(color(white)) ///
	title(Behavior in Elementary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_elem_allbehavior.png", replace

clear
svmat AUTObig
gen year = _n

graph twoway (connect AUTObig3 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTObig5 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)), ///
	graphregion(color(white)) xlabel(0(1)6,nogrid) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2) lab(1 Learning Skills GPA) lab(2 Effort GPA) cols(2)) graphregion(color(white)) ///
	title(Learning Skills in Elementary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_elem_alllearning.png", replace


clear
svmat AUTObig
gen year = _n

graph twoway (connect AUTObig1 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTObig2 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)), ///
	graphregion(color(white)) xlabel(0(1)6,nogrid) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2) lab(1 Math ) lab(2 English) cols(2)) graphregion(color(white)) ///
	title(Tests in Elementary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_elem_alltests.png", replace


restore
					
*****************************************************
*** Figure: High School VA Autocorrelations ***
*****************************************************
****** Create factors ******
preserve
use ../data/lausd_data/vascores/hbiastest_studentlevel_HS_math, clear

*sort studentid year
*merge studentid year using ../data/lausd_data/vascores/hbiastest_studentlevel_HS_english

keep if grade >= 6 & grade <= 11 
drop if teacherid == . 

*** Make teacher-year level dataset 
collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstmath_math gpatotal_math logdaysabsent_math suspended_math heldback_math {
		egen fn_`var' = std(score_r_`var')
}

gen score_r_jackson_factor = fn_gpatotal_math - fn_logdaysabsent_math - fn_suspended_math - fn_heldback_math

foreach var in zcstmath_math gpatotal_math logdaysabsent_math suspended_math heldback_math {
		drop fn_`var' 
}

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

save ../data/lausd_data/vascores/temp_hsmath, replace 


use ../data/lausd_data/vascores/hbiastest_studentlevel_HS_english, clear

keep if grade >= 6 & grade <= 11 
drop if teacherid == . 

*** Make teacher-year level dataset 
collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstela_english gpatotal_english logdaysabsent_english suspended_english heldback_english {
		egen fn_`var' = std(score_r_`var')
}

gen score_r_jackson_factor = fn_gpatotal_english - fn_logdaysabsent_english - fn_suspended_english - fn_heldback_english

foreach var in zcstela_english gpatotal_english logdaysabsent_english suspended_english heldback_english {
		drop fn_`var' 
}

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

save ../data/lausd_data/vascores/temp_hsenglish, replace 

use ../data/lausd_data/vascores/temp_hsmath, clear
append using ../data/lausd_data/vascores/temp_hsenglish
drop if score_r_jackson_factor == . & score_r_zcstmath_math  == . & score_r_zcstela_english == . 

gen score_r_cog_factor = score_r_zcstmath_math 
replace score_r_cog_factor = score_r_zcstela_english if score_r_cog_factor == . 

collapse (mean) score_r*, by(teacherid year)

foreach var of varlist *zcst*{
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

******* Create Figure **********
*** Create lagged  
xtset teacherid year 
foreach var in score_r_jackson_factor score_r_cog_factor ///
	score_r_zcstmath_math score_r_gpatotal_math score_r_logdaysabsent_math score_r_suspended_math score_r_heldback_math ///
	score_r_zcstela_english score_r_gpatotal_english score_r_logdaysabsent_english score_r_suspended_english score_r_heldback_english {
		gen l1`var' = L1.`var'
		gen l2`var' = L2.`var'
		gen l3`var' = L3.`var'
		gen l4`var' = L4.`var'
		gen l5`var' = L5.`var'
		gen l6`var' = L6.`var'
		gen l7`var' = L7.`var'
}

*** Auto correlation matrix for the three factors
local lags = 6
matrix AUTO = J(`lags',2,.)

local column = 1 
foreach var in score_r_cog_factor score_r_jackson_factor {
		di "*****"
		di "`var'" 
		di "*****"
		local row = 1
		forvalues x = 1/`lags' { 
			corr `var' l`x'`var'
			mat AUTO[`row',`column'] = r(rho)
			local ++row
		}
		local ++column
}

*** Auto correlation matrix for all the inputs
local lags = 6
matrix AUTObig = J(`lags',10,.)

local column = 1 
foreach var in score_r_zcstmath_math score_r_gpatotal_math score_r_logdaysabsent_math score_r_suspended_math score_r_heldback_math ///
	score_r_zcstela_english score_r_gpatotal_english score_r_logdaysabsent_english score_r_suspended_english score_r_heldback_english {
		di "*****"
		di "`var'" 
		di "*****"
		local row = 1
		forvalues x = 1/`lags' { 
			corr `var' l`x'`var'
			mat AUTObig[`row',`column'] = r(rho)
			local ++row
		}
		local ++column
}


clear
svmat AUTO
gen year = _n

graph twoway (connect AUTO1 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTO2 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)), ///
	graphregion(color(white)) xlabel(0(1)6) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2) lab(1 Test Score) lab(2 Behavior)) graphregion(color(white)) ///
	title(Secondary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_hs.png", replace


*** The pieces of the math teacher
clear
svmat AUTObig
gen year = _n

graph twoway (connect AUTObig1 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTObig2 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)) ///
	(connect AUTObig3 year, mcolor(black) lcolor(black) msymbol(triangle) lpattern(dot)) ///
	(connect AUTObig4 year, mcolor(black) lcolor(black) msymbol(diamond) lpattern(dash_dot)) ///
	(connect AUTObig5 year, mcolor(black) lcolor(black) msymbol(plus) lpattern(longdash)), ///
	graphregion(color(white)) xlabel(0(1)6) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2 3 4 5) lab(1 Test Score) lab(2 GPA) lab(3 Absences) lab(4 Suspended) lab(5 Heldback) col(3)) graphregion(color(white)) ///
	title(Secondary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_hs_allmath.png", replace


*** The pieces of the English teacher
clear
svmat AUTObig
gen year = _n

graph twoway (connect AUTObig6 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTObig7 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)) ///
	(connect AUTObig8 year, mcolor(black) lcolor(black) msymbol(triangle) lpattern(dot)) ///
	(connect AUTObig9 year, mcolor(black) lcolor(black) msymbol(diamond) lpattern(dash_dot)) ///
	(connect AUTObig10 year, mcolor(black) lcolor(black) msymbol(plus) lpattern(longdash)), ///
	graphregion(color(white)) xlabel(0(1)6) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2 3 4 5) lab(1 Test Score) lab(2 GPA) lab(3 Absences) lab(4 Suspended) lab(5 Heldback) col(3)) graphregion(color(white)) ///
	title(Secondary School, color(black)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_hs_allenglish.png", replace



restore

*************************************************
*** Dot Plots ***
*************************************************
cap program drop reg_dotplot
program reg_dotplot
	syntax [if], depvar(string) indvar(string) ctrl(string)  cluster(string) /// 
		title(string) ylabels(string) xtitle(string) output(string) ///
		[absorb(string) cats(string) cats_ctrl(string) stitle(string) notrend(string) postfix(string) sdfactor(string) span(string)]
		
	
	local num_xpts=wordcount("`indvar'") 

	file open tables using "..\data\lausd_data\temp\reg_dotplot_values.txt", write replace
	file write tables ("pt") _tab
	file write tables ("cil") _tab
	file write tables ("ciu") _n
	
	local sd = 0

	di "`var'"
	cap sum `depvar'
	if "`span'"==""{
		local sd = max(`sd',r(sd))
	}
	if "`absorb'"!=""{
		areg `depvar' `indvar' `ctrl' `if', cluster(`cluster') absorb(`absorb')
	}
	if "`absorb'"==""{
		reg `depvar' `indvar' `ctrl'  `if', cluster(`cluster')
	}
	
	if "`sdfactor'"==""{
		local sdfactor=.01
	}
	if "`span'"!=""{
		local minvalue = word("`span'",1)
		local maxvalue = word("`span'",2)
	}
	if "`span'"==""{
		local minvalue = `sdfactor'*`sd'
		local maxvalue = -2/3*`sdfactor'*`sd' 
	}
	foreach var in `indvar' {
		file write tables (_b[`var']) _tab
		file write tables (_b[`var'] - 2*_se[`var']) _tab
		file write tables (_b[`var'] + 2*_se[`var']) _n   
	}
	file close tables
	preserve
	cap insheet using "..\data\lausd_data\temp\reg_dotplot_values.txt", clear tab
	cap gen order = _n *-1
    cap levelsof order, local(numlist) clean
	foreach n in `numlist'{
		    local num = -1*`n'
			local word = subinstr(word("`ylabels'",`num') +" " + `"`postfix'"',"_"," ",.)
			local axislabel `axislabel' `n' `"`word'"'  
	}	
		
	twoway (scatter order pt, mcolor(black)) (rcap cil ciu order, horizontal lcolor(black)), ///
		yline(`numlist', lpattern(shortdash) lcolor(gs12)) ///
		title("`title'") ///
		subtitle("`stitle'") ///
		ytitle("") ///
		ylabel(`axislabel', ang(h)) ///
		xtitle(`xtitle') ///
		legend(off) ///
		xscale(range(`minvalue' `maxvalue')) ///
		xlabel(#4) ///
		xline(0, lcolor(black)) ///
		graphregion(color(white))
		graph export ../output/`output'.png, as(png) replace
	restore
end

#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

** Value Added
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zalltest) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("All Tests") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	span(-.06 .06) ///
	output(dot_gpaVA_hs_zalltest)

reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_znontest) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Non-Test Score") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	span(-.06 .06) ///
	output(dot_gpaVA_hs_znontest)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zcahsee) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("CAHSEE Test Scores") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	span(-.06 .06) ///
	output(dot_gpaVA_hs_zcahsee)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zbehave) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Behavior") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	span(-.06 .06) ///
	output(dot_gpaVA_hs_zbehave)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zbehavlong) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Behavior") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	span(-.06 .06) ///
	output(dot_gpaVA_zbehavlong)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zgpa) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("GPA") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	span(-.06 .06) ///
	output(dot_gpaVA_hs_zgpa)

reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_dropout) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("HS Drop Out") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_dropout)

reg_dotplot if grade >= 3 & grade <= 5, depvar(eversat) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Took SAT") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_eversat)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(satscoretotal) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("SAT Score") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_satscoretotal)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_gpatotal) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("HS GPA") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_gpatotal)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_workeffortgpatotal) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("HS Effort GPA") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_workeffortgpatotal)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_coopgpatotal) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("HS Cooperation GPA") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_coopgpatotal)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(zcahseescoremath) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("CAHSEE Math Score") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_zcahseescoremath)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(zcahseescoreela) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("CAHSEE English Score") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_zcahseescoreela)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_dayssuspended) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Days Suspended in HS") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_dayssuspended)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_logdaysabsent) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Log Days Absent in HS") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_logdaysabsent)
	
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_heldback) /// 
	indvar(ztva_gpamath ztva_gparead ztva_gpawrite ztva_gpalisten ztva_gpaspeak ztva_gpasocial ztva_gpascience ztva_gpahealth ztva_gpape ztva_gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Heldback in HS") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA Value Added) ///
	output(dot_gpaVA_hs_heldback)	
  	
** Predictions
reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zalltest) /// 
	indvar(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("All Test Scores") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA) ///
	output(dot_gpapredict_hs_zalltest)

reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_znontest) /// 
	indvar(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Non-Test Scores") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA) ///
	output(dot_gpapredict_hs_znontest)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zcahsee) /// 
	indvar(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("CAHSEE Test Scores") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA) ///
	output(dot_gpapredict_hs_zcahsee)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zbehave) /// 
	indvar(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Behavior") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA) ///
	output(dot_gpapredict_hs_zbehave)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zbehavlong) /// 
	indvar(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("Behavior") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA) ///
	output(dot_gpapredict_zbehavlong)


reg_dotplot if grade >= 3 & grade <= 5, depvar(hs_zgpa) /// 
	indvar(gpamath gparead gpawrite gpalisten gpaspeak gpasocial gpascience gpahealth gpape gpaart) ///
	ctrl(`ctrls') cluster(hs_cdscode) ///
	title("GPA") ///
	postfix(GPA) /// 
	ylabels("Math Reading Writing Listening Speaking Social_Science Science Health PE Art") ///
	xtitle(Coefficient on GPA) ///
	output(dot_gpapredict_hs_zgpa)

     



*************************************************
*** Other stats for the paper or presentation ***
*************************************************

*************************************************************
*** How much variation is within school or between school ***
*************************************************************
use ../data/lausd_data/vascores/biastest_studentlevel, clear

keep if grade >= 2 & grade <= 5 
drop if teacherid == . 

*** Teacher VA factors 
** Cog
factor score_r_zcstmath score_r_zcstela 
rotate 
predict score_r_cog_factor

** Jackson
factor score_r_gpatotal score_r_logdaysabsent score_r_suspended score_r_heldback
rotate 
predict score_r_jackson_factor
replace score_r_jackson_factor = -1*score_r_jackson_factor

** Noncog
factor score_r_effmarktotalyr score_r_habmarktotalyr
rotate 
predict score_r_noncog_factor 


foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarktotalyr cog_factor jackson_factor noncog_factor {
		egen va_`var' = mean(score_r_`var'), by(teacherid year)
}	
		
sort teacherid year
drop if teacherid == teacherid[_n-1] & year == year[_n-1]


foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarktotalyr cog_factor jackson_factor noncog_factor {
		areg va_`var', a(cdscode)	
}




**************************************************
**** Create additional graphs for presentation ***
**************************************************
clear all
set more off
set sortseed 1
set seed 123456

* Set working directory
*cd "/Users/nolan/Dropbox/research/noncog_va/analysis/"

use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade


#delimit ;
local standard_va_vars "zcstmath zcstela";


local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

************************************************************
**** Binscatter teacher factor VA by Actual HS outcome *****
************************************************************
/*

** Create grade 11 math and Enligh CST scores
egen tempvar = mean(zcstmath), by(studentid grade)
replace tempvar = . if grade != 11
egen zcstmathg11 = mean(tempvar), by(studentid)
drop tempvar 

egen tempvar = mean(zcstela), by(studentid grade)
replace tempvar = . if grade != 11
egen zcstelag11 = mean(tempvar), by(studentid)
drop tempvar 

** Create postition local for each variable-factor-(actual, predicted, or both) binscatter graph
** Set the defalt position for each graph to 5 
foreach var in evergrad hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 zcstelag11 zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
   		local fac = substr("`factor'", 6, 3)
		local pa_`var'_`fac' = 5
		local pp_`var'_`fac' = 5
		local pb_`var'_`fac' = 5
		
	}
}

** Changes for actual 
local pa_evergrad_cog = 1
local pa_eversat_cog = 11
local pa_hs_logdaysabsent_cog = 5
local pa_hs_dropout_jac = 5
local pa_hs_dayssuspended_jac = 1
local pa_hs_fracdaysabsent_jac = 1
local pa_hs_heldback_jac = 1
local pa_apclasstotal_jac = 11
local pa_hs_logdaysabsent_jac = 1
local pa_hs_dayssuspended_non = 1
local pa_hs_fracdaysabsent_non = 1
local pa_hs_heldback_non = 5
local pa_everpsat_non = 11
local pa_hs_dropout_non = 5
local pa_hs_logdaysabsent_non = 1
local pa_hs_workeffortgpatotal_non = 1
local pa_hs_coopgpatotal_non = 7

** Changes for predicted
local pp_hs_dayssuspended_cog = 1 
local pp_hs_fracdaysabsent_cog = 1
local pp_hs_heldback_cog = 1
local pp_hs_logdaysabsent_cog = 1
local pp_evergrad_jac = 1
local pp_hs_gpatotal_jac = 1 
local pp_psatscoreoverall_jac = 11
local pp_zcstscience5_jac = 1
local pp_zcstscience8_jac = 1
local pp_zcstsocial8_jac = 1
local pp_zcstsocial11_jac = 1
local pp_zcstsocialworld_jac = 1
local pp_evergrad_non = 1
local pp_psatscoreoverall_non = 1

** Changes for both 
local pb_evergrad_cog = 1
local pb_eversat_cog = 11
local pb_hs_dropout_cog = 7
local pb_hs_heldback_cog = 11
local pb_hs_logdaysabsent_cog = 1
local pb_apclasstotal_jac = 11
local pb_everpsat_jac = 7
local pb_hs_dayssuspended_jac = 1
local pb_hs_dropout_jac = 7 
local pb_fracdaysabsent_jac = 1 
local pb_hs_heldback_jac = 1
local pb_hs_logdaysabsent_jac = 1
local pb_everpsat_non = 7
local pb_hs_dayssuspended_non = 1
local pb_fracdaysabsent_non = 1
local pb_hs_heldback_non = 1
local pb_hs_logdaysabsent_non = 1	

** Variable Labels
label var evergrad "HS Graduate" 
label var hs_gpatotal "HS GPA"
label var eversat "Took SAT"
label var satscoretotal "SAT Score"
label var hs_dayssuspended "Days Suspended in HS" 
label var hs_fracdaysabsent "Fraction Days Absent in HS"
label var hs_logdaysabsent "Log Days Absent in HS"
label var hs_heldback "Heldback in HS"
label var zcahseescoremath "CAHSEE Math Score"
label var zcahseescoreela "CAHSEE English Score" 
label var hs_dropout "HS Dropout" 
label var apclasstotal "Number of AP Courses" 
label var zcstscience10 "Grade 10 Science CST Score" 
label var zcstsocial11 "Grade 11 Social Science CST Score" 
label var hs_workeffortgpatotal "HS Effort GPA"
label var hs_coopgpatotal "HS Cooperation GPA"
label var everpsat "Took PSAT"
label var psatscoreoverall "PSAT Score"
label var zcstmath "Math CST Score"
label var zcstela "English CST Score"
label var zcstsocial8 "Grade 8 Social Science CST Score" 
label var zcstsocialworld "World History CST Score" 
label var zcstscience5 "Grade 5 Science CST Score"
label var zcstscience8 "Grade 8 Science CST Score"
label var zcstmathg11 "Grade 11 Math CST Score"
label var zcstelag11 "Grade 11 English CST Score"

label var ztva_cog_factor "Test Score Value-Added"
label var ztva_jackson_factor "Behavior Value-Added"
label var ztva_noncog_factor "Learning Skills Value-Added"

** Variable for ylabel of y-axis
local satscoretotalyub = 1360
local satscoretotalylb = 1320
local satscoretotalinterval = 10
local hs_gpatotalyub = 2.34
local hs_gpatotalylb = 2.26
local hs_gpatotalinterval = 0.02
local hs_logdaysabsentyub = 2.55
local hs_logdaysabsentylb = 2.45
local hs_logdaysabsentinterval = 0.05


*** note("Coefficient = `coeff'%" "(`se')", position(5) ring(0) size(medium))
*** if "`var'" == "incar_totalmin"
*** legend(order("white sex ratio" 2 "black sex ratio"), postition(10) ring(0))
*legend(on order(- "asdfas") pos(11) ring(0)) ///

#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

*** Binscatter plots
foreach depvar in satscoretotal hs_gpatotal hs_logdaysabsent { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
	   	reg `depvar' `factor' `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	   	local stars ""
		local space ""
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.1 {
			local stars "*"
			local space " "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.05 {
			local stars "**"
			local space "  "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.01 {
			local stars "***"
			local space "    "
		}
		local tempcoef: display %5.3fc _b[`factor']
	   	di `tempcoef'
	   	local tempse: display %5.3fc _se[`factor'] 
	   	di `tempse'
	   	local fac = substr("`factor'", 6, 3)
		binscatter `depvar' `factor' if grade >= 3 & grade <= 5, controls(`ctrls') mcolors(black) lcolors(black) ///
	   		ytitle(`: variable label `depvar'', color(black)) xtitle(`: variable label `factor'', color(black)) ///
	   		title(`: variable label `depvar'', color(black)) ylabel(``depvar'ylb'(``depvar'interval')``depvar'yub',nogrid) xlabel(-2(1)2) ///
	    	note("Coef = `tempcoef'`stars' " "(`tempse')`space'", box fcolor(none) placement(`pa_`depvar'_`fac'') ring(0) justification(right) size(large)) ///
	    	savegraph(../output/binscatter_`factor'_`depvar'_forslides.png) savedata(../output/binscatterdata/actual_`factor'_`depvar'_forslides.dta) replace 
	}
}	   	

*/

*********
***********
*********
** Binscatter for presentations
********
********
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

set scheme s2color

fvset base 2007 year
fvset base 3 grade
drop j_z*


gen cahseescoreoverall = zcahseescoremath + zcahseescoreela
egen zcahseescoreoverallmean = std(cahseescoreoverall)

egen cahseescoreoverallmean = mean(cahseescoreoverall), by(year)
egen cahseescoreoverallsd = sd(cahseescoreoverall), by(year)
gen zcahseescoreoverall = (cahseescoreoverall - cahseescoreoverallmean)/cahseescoreoverallsd

drop cahseescoreoverall


#delimit ;
local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";

#delimit cr

** Create postition local for each variable-factor-(actual, predicted, or both) binscatter graph
** Set the defalt position for each graph to 5 
foreach var in evergrad hs_gpatotal eversat satscoretotal hs_dayssuspended hs_fracdaysabsent hs_logdaysabsent hs_heldback hs_dropout ///
  zcahseescoremath zcahseescoreela apclasstotal zcstscience10 zcstsocial11 hs_workeffortgpatotal hs_coopgpatotal ///
  everpsat psatscoreoverall zcstmathg11 zcstelag11 zcstsocial8 zcstsocialworld zcstscience5 zcstscience8 { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {
   		local fac = substr("`factor'", 6, 3)
		local pa_`var'_`fac' = 5
		local pp_`var'_`fac' = 5
		local pb_`var'_`fac' = 5
		
	}
}

** Changes for actual 
local pa_evergrad_cog = 1
local pa_eversat_cog = 11
local pa_hs_logdaysabsent_cog = 5
local pa_hs_dropout_jac = 5
local pa_hs_dayssuspended_jac = 1
local pa_hs_fracdaysabsent_jac = 1
local pa_hs_heldback_jac = 1
local pa_apclasstotal_jac = 11
local pa_hs_logdaysabsent_jac = 1
local pa_hs_dayssuspended_non = 1
local pa_hs_fracdaysabsent_non = 1
local pa_hs_heldback_non = 5
local pa_everpsat_non = 11
local pa_hs_dropout_non = 5
local pa_hs_logdaysabsent_non = 1
local pa_hs_workeffortgpatotal_non = 1
local pa_hs_coopgpatotal_non = 7

** Changes for predicted
local pp_hs_dayssuspended_cog = 1 
local pp_hs_fracdaysabsent_cog = 1
local pp_hs_heldback_cog = 1
local pp_hs_logdaysabsent_cog = 1
local pp_evergrad_jac = 1
local pp_hs_gpatotal_jac = 1 
local pp_psatscoreoverall_jac = 11
local pp_zcstscience5_jac = 1
local pp_zcstscience8_jac = 1
local pp_zcstsocial8_jac = 1
local pp_zcstsocial11_jac = 1
local pp_zcstsocialworld_jac = 1
local pp_evergrad_non = 1
local pp_psatscoreoverall_non = 1

** Changes for both 
local pb_evergrad_cog = 1
local pb_eversat_cog = 11
local pb_hs_dropout_cog = 7
local pb_hs_heldback_cog = 11
local pb_hs_logdaysabsent_cog = 1
local pb_apclasstotal_jac = 11
local pb_everpsat_jac = 7
local pb_hs_dayssuspended_jac = 1
local pb_hs_dropout_jac = 7 
local pb_fracdaysabsent_jac = 1 
local pb_hs_heldback_jac = 1
local pb_hs_logdaysabsent_jac = 1
local pb_everpsat_non = 7
local pb_hs_dayssuspended_non = 1
local pb_fracdaysabsent_non = 1
local pb_hs_heldback_non = 1
local pb_hs_logdaysabsent_non = 1	

** Variable Labels
label var evergrad "HS Graduate" 
label var hs_gpatotal "HS GPA"
label var eversat "Took SAT"
label var satscoretotal "SAT Score"
label var hs_dayssuspended "Days Suspended in HS" 
label var hs_fracdaysabsent "Fraction Days Absent in HS"
label var hs_logdaysabsent "Log Days Absent in HS"
label var hs_heldback "Heldback in HS"
label var zcahseescoremath "CAHSEE Math Score"
label var zcahseescoreela "CAHSEE English Score" 
label var hs_dropout "HS Dropout" 
label var apclasstotal "Number of AP Courses" 
label var zcstscience10 "Grade 10 Science CST Score" 
label var zcstsocial11 "Grade 11 Social Science CST Score" 
label var hs_workeffortgpatotal "HS Effort GPA"
label var hs_coopgpatotal "HS Cooperation GPA"
label var everpsat "Took PSAT"
label var psatscoreoverall "PSAT Score"
label var zcstmath "Math CST Score"
label var zcstela "English CST Score"
label var zcstsocial8 "Grade 8 Social Science CST Score" 
label var zcstsocialworld "World History CST Score" 
label var zcstscience5 "Grade 5 Science CST Score"
label var zcstscience8 "Grade 8 Science CST Score"


label var ztva_cog_factor "Test Score Value-Added"
label var ztva_jackson_factor "Behavior Value-Added"
label var ztva_noncog_factor "Learning Skills Value-Added"

** Variable for ylabel of y-axis
local satscoretotalyub = 1360
local satscoretotalylb = 1320
local satscoretotalinterval = 10
local hs_gpatotalyub = 2.34
local hs_gpatotalylb = 2.26
local hs_gpatotalinterval = 0.02
local hs_logdaysabsentyub = 2.55
local hs_logdaysabsentylb = 2.45
local hs_logdaysabsentinterval = 0.05

	
** Binscatter Plot		
foreach depvar in hs_gpatotal satscoretotal { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {	

		reg `depvar' `factor' `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	   	local stars ""
		local space ""
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.1 {
			local stars "*"
			local space " "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.05 {
			local stars "**"
			local space "  "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.01 {
			local stars "***"
			local space "    "
		}
		local tempcoef: display %5.3fc _b[`factor']
	   	di `tempcoef'
	   	local tempse: display %5.3fc _se[`factor'] 
	   	di `tempse'
		
		preserve
		
		use "../output/binscatterdata/actual_ztva_cog_factor_`depvar'.dta", clear
		sum `depvar' 
		local `depvar'mean = r(mean)
		di ``depvar'mean'
		
		use "../output/binscatterdata/actual_`factor'_`depvar'.dta", clear
		sum `depvar'
		local `depvar'gap = ``depvar'mean' - r(mean)
		replace `depvar' = `depvar' + ``depvar'gap'

		
		cap label var evergrad "HS Graduate" 
		cap label var hs_gpatotal "HS GPA"
		cap label var eversat "Took SAT"
		cap label var satscoretotal "SAT Score"
		cap label var hs_dayssuspended "Days Suspended in HS" 
		cap label var hs_fracdaysabsent "Fraction Days Absent in HS"
		cap label var hs_logdaysabsent "Log Days Absent in HS"
		cap label var hs_heldback "Heldback in HS"
		cap label var zcahseescoremath "CAHSEE Math Score"
		cap label var zcahseescoreela "CAHSEE English Score" 
		cap label var hs_dropout "HS Dropout" 
		cap label var apclasstotal "Number of AP courses" 
		cap label var zcstscience10 "Grade 10 Science CST Score" 
		cap label var zcstsocial11 "Grade 11 Social Science CST Score" 
		cap label var hs_workeffortgpatotal "HS Effort GPA"
		cap label var hs_coopgpatotal "HS Cooperation GPA"
		cap label var everpsat "Took PSAT"
		cap label var psatscoreoverall "PSAT Score"
		cap label var zcstmath "Math CST Score"
		cap label var zcstela "English CST Score"
		cap label var zcstsocial8 "Grade 8 Social Science CST Score" 
		cap label var zcstsocialworld "World History CST Score" 
		cap label var zcstscience5 "Grade 5 Science CST Score"
		cap label var zcstscience8 "Grade 8 Science CST Score"
		cap label var zcstmathg11 "Grade 11 Math CST Score"
		cap label var zcstelag11 "Grade 11 English CST Score"

		cap label var ztva_cog_factor "Test Score Value-Added"
		cap label var ztva_jackson_factor "Behavior Value-Added"
		cap label var ztva_noncog_factor "Learning Skills Value-Added"
		
		graph twoway (scatter `depvar' `factor', mcolor(black) lcolor(black) msymbol(circle)) ///
			(lfit `depvar' `factor', lpattern(solid) lcolor(black)), ///
			legend(off) graphregion(color(white)) ///
			note("Coef = `tempcoef'`stars' " "(`tempse')`space'", box fcolor(none) placement(`pa_`depvar'_`fac'') ring(0) justification(right) size(large)) ///
	    	title(`: variable label `depvar'', color(black)) ylabel(``depvar'ylb'(``depvar'interval')``depvar'yub',nogrid) xlabel(-2(1)2) ///
	    	ytitle(`: variable label `depvar'', color(black)) xtitle(`: variable label `factor'', color(black))  
		graph export "../output/binscatter_`factor'_`depvar'_forslides2.png", replace

		restore

	}
}



*** Binscatter Plot	
** Log absences	
foreach depvar in hs_logdaysabsent { 
	foreach factor in ztva_cog_factor ztva_jackson_factor ztva_noncog_factor {	

		reg `depvar' `factor' `ctrls' if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	   	local stars ""
		local space ""
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.1 {
			local stars "*"
			local space " "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.05 {
			local stars "**"
			local space "  "
		}
		if 2*ttail(e(df_r), abs(_b[`factor']/_se[`factor'])) < 0.01 {
			local stars "***"
			local space "    "
		}
		local tempcoef: display %5.3fc _b[`factor']
	   	di `tempcoef'
	   	local tempse: display %5.3fc _se[`factor'] 
	   	di `tempse'
		
		preserve
		
		use "../output/binscatterdata/actual_ztva_jackson_factor_`depvar'.dta", clear
		sum `depvar' 
		local `depvar'mean = r(mean)
		di ``depvar'mean'
		
		use "../output/binscatterdata/actual_`factor'_`depvar'.dta", clear
		sum `depvar'
		local `depvar'gap = ``depvar'mean' - r(mean)
		replace `depvar' = `depvar' + ``depvar'gap'

		
		cap label var evergrad "HS Graduate" 
		cap label var hs_gpatotal "HS GPA"
		cap label var eversat "Took SAT"
		cap label var satscoretotal "SAT Score"
		cap label var hs_dayssuspended "Days Suspended in HS" 
		cap label var hs_fracdaysabsent "Fraction Days Absent in HS"
		cap label var hs_logdaysabsent "Log Days Absent in HS"
		cap label var hs_heldback "Heldback in HS"
		cap label var zcahseescoremath "CAHSEE Math Score"
		cap label var zcahseescoreela "CAHSEE English Score" 
		cap label var hs_dropout "HS Dropout" 
		cap label var apclasstotal "Number of AP courses" 
		cap label var zcstscience10 "Grade 10 Science CST Score" 
		cap label var zcstsocial11 "Grade 11 Social Science CST Score" 
		cap label var hs_workeffortgpatotal "HS Effort GPA"
		cap label var hs_coopgpatotal "HS Cooperation GPA"
		cap label var everpsat "Took PSAT"
		cap label var psatscoreoverall "PSAT Score"
		cap label var zcstmath "Math CST Score"
		cap label var zcstela "English CST Score"
		cap label var zcstsocial8 "Grade 8 Social Science CST Score" 
		cap label var zcstsocialworld "World History CST Score" 
		cap label var zcstscience5 "Grade 5 Science CST Score"
		cap label var zcstscience8 "Grade 8 Science CST Score"
		cap label var zcstmathg11 "Grade 11 Math CST Score"
		cap label var zcstelag11 "Grade 11 English CST Score"

		cap label var ztva_cog_factor "Test Score Value-Added"
		cap label var ztva_jackson_factor "Behavior Value-Added"
		cap label var ztva_noncog_factor "Learning Skills Value-Added"
		
		graph twoway (scatter `depvar' `factor', mcolor(black) lcolor(black) msymbol(circle)) ///
			(lfit `depvar' `factor', lpattern(solid) lcolor(black)), ///
			legend(off) graphregion(color(white)) ///
			note("Coef = `tempcoef'`stars' " "(`tempse')`space'", box fcolor(none) placement(`pa_`depvar'_`fac'') ring(0) justification(right) size(large)) ///
	    	title(`: variable label `depvar'', color(black)) ylabel(``depvar'ylb'(``depvar'interval')``depvar'yub',nogrid) xlabel(-2(1)2) ///
	    	ytitle(`: variable label `depvar'', color(black)) xtitle(`: variable label `factor'', color(black))  
		graph export "../output/binscatter_`factor'_`depvar'_forslides2.png", replace

		restore

	}
}
		
****** Elementary school autocorrelation

*****************************************************
*** Figure: Elementary School VA Autocorrelations ***
*****************************************************
preserve 
use ../data/lausd_data/vascores/biastest_studentlevel, clear

*use year studentid grade teacherid cdscode ztva_cog_factor ztva_jackson_factor ztva_noncog_factor ///
*	ztva_zcstmath ztva_zcstela ztva_gpatotal ztva_logdaysabsent ztva_suspended ztva_heldback ///
*	ztva_effmarktotalyr ztva_habmarkfirst5totalyr ztva_habmarklast9totalyr using ../data/lausd_data/analysis/student_year_main, clear

keep if grade >= 2 & grade <= 5 
drop if teacherid == . 

*** Make teacher-year level dataset 
collapse (mean) score_r_*, by(teacherid year)
xtset teacherid year

*** Even weighted VA factors 
foreach var in zcstmath zcstela gpatotal logdaysabsent suspended heldback ///
	effmarktotalyr habmarkfirst5totalyr habmarklast9totalyr {
		egen fn_`var' = std(score_r_`var')
}

gen score_r_cog_factor = fn_zcstmath + fn_zcstela
gen score_r_jackson_factor_even = fn_gpatotal - fn_logdaysabsent - fn_suspended - fn_heldback
gen score_r_noncog_factor_even = fn_effmarktotalyr + fn_habmarkfirst5totalyr + fn_habmarklast9totalyr

*** Scale Factors by year
*Note that factors are scaled to be mean zero across all years, but not SD 1
foreach var of varlist *factor* score_r* {
	egen sd=sd(`var'), by(year)
	egen mean=mean(`var'), by(year)
	replace `var' = (`var'-mean)/sd
	drop mean sd
}

*** Create lagged 
foreach var in score_r_cog_factor score_r_jackson_factor score_r_noncog_factor ///
	score_r_zcstmath score_r_zcstela score_r_habmarktotalyr score_r_gpatotal ///
	score_r_effmarktotalyr score_r_logdaysabsent score_r_suspended score_r_heldback {
		gen l1`var' = L1.`var'
		gen l2`var' = L2.`var'
		gen l3`var' = L3.`var'
		gen l4`var' = L4.`var'
		gen l5`var' = L5.`var'
		gen l6`var' = L6.`var'
		gen l7`var' = L7.`var'
}

*** Auto correlation matrix for the three factors
local lags = 6
matrix AUTO = J(`lags',3,.)

local column = 1 
foreach var in score_r_cog_factor score_r_jackson_factor score_r_noncog_factor {
*	score_r_zcstmath_math score_r_zcstela_english ///
*	score_r_gpatotal_math score_r_logdaysabsent_math score_r_suspended_math score_r_heldback_math /// 
*	score_r_gpatotal_english score_r_logdaysabsent_english score_r_suspended_english score_r_heldback_english
		di "*****"
		di "`var'" 
		di "*****"
		local row = 1
		forvalues x = 1/`lags' { 
			corr `var' l`x'`var'
			mat AUTO[`row',`column'] = r(rho)
			local ++row
		}
		local ++column
}

*** Auto correlation matrix for all the inputs
local lags = 6
matrix AUTObig = J(`lags',8,.)

local column = 1 
foreach var in score_r_zcstmath score_r_zcstela score_r_habmarktotalyr score_r_gpatotal ///
	score_r_effmarktotalyr score_r_logdaysabsent score_r_suspended score_r_heldback {
		di "*****"
		di "`var'" 
		di "*****"
		local row = 1
		forvalues x = 1/`lags' { 
			corr `var' l`x'`var'
			mat AUTObig[`row',`column'] = r(rho)
			local ++row
		}
		local ++column
}

clear
svmat AUTO
gen year = _n

graph twoway (connect AUTO1 year, mcolor(black) lcolor(black) msymbol(circle) lpattern(solid)) ///
	(connect AUTO2 year, mcolor(black) lcolor(black) msymbol(square) lpattern(dash)) ///
	(connect AUTO3 year, mcolor(black) lcolor(black) msymbol(triangle) lpattern(dot)), ///
	graphregion(color(white)) xlabel(0(1)6,nogrid) ylabel(0(.1).6,nogrid) ///
	legend(order(1 2 3) lab(1 Test Score) lab(2 Behavior) lab(3 Learning) cols(3)) graphregion(color(white)) ///
   	ytitle(Correlation, color(black)) xtitle(Years Between Classes, color(black))   
graph export "../output/autocorrelations_elem_forslides.png", replace

**** Interactions

**********************************************************
*** TABLE 7x: Causal estimates using 3 factor variables ***
**********************************************************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade


#delimit ;
local standard_va_vars "zcstmath zcstela";


local ctrlstable7x "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

cap file close tables 
file open tables using "../output/table7x_vasmall.txt", write replace
file write tables "<tab:vasmallx>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' c.ztva_cog_factor##c.F.ztva_cog_factor c.ztva_jackson_factor##F.c.ztva_jackson_factor c.ztva_noncog_factor##F.c.ztva_noncog_factor ///
		`ctrlstable7x'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table7x_vasmall.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor F.ztva_cog_factor c.ztva_cog_factor#cF.ztva_cog_factor ///
	ztva_jackson_factor F.ztva_jackson_factor c.ztva_jackson_factor#cF.ztva_jackson_factor ///
	ztva_noncog_factor F.ztva_noncog_factor c.ztva_noncog_factor#cF.ztva_noncog_factor)	
	
**********************************************************
*** TABLE 7y: effect on next year teacher VA ***
**********************************************************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade


#delimit ;
local standard_va_vars "zcstmath zcstela";


local ctrlstable7y "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

cap file close tables 
file open tables using "../output/table7y_vasmall.txt", write replace
file write tables "<tab:vasmally>" _n
cap file close tables 

local counter = 1
local modellist = ""
	
reg ztva_cog_factor L.ztva_cog_factor L.ztva_jackson_factor L.ztva_noncog_factor ///
	`ctrlstable7y'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
		
reg ztva_jackson_factor L.ztva_cog_factor L.ztva_jackson_factor L.ztva_noncog_factor ///
	`ctrlstable7y'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
		
reg ztva_noncog_factor L.ztva_cog_factor L.ztva_jackson_factor L.ztva_noncog_factor ///
	`ctrlstable7y'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
		
**** 12th grade summary statistics
sum gpatotal if grade == 12
sum dayssuspended if grade == 12
sum heldback if grade == 11


	
**********************************************************
*** TABLE 41: Interaction Effect between Test Score and Behavioral Value-Added ***
**********************************************************
use ../data/lausd_data/analysis/student_year_main, clear
xtset studentid year

fvset base 2007 year
fvset base 3 grade

gen ztva_cog_jack_interaction = ztva_cog_factor*ztva_jackson_factor


#delimit ;
local standard_va_vars "zcstmath zcstela";


local ctrls "i.grade#C.(L.zcstmath L.zcstela L.gpatotal L.effmarktotalyr L.zcstmath? 
	L.zcstela? L.gpatotal? L.effmarktotalyr? L.logdaysabsent L.suspended L.heldback)
	i.grade#C.(grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size) 
	ell i.grade i.year";


local controls_import "zcstmath zcstela gpatotal effmarktotalyr zcstmath? 
	zcstela? gpatotal? effmarktotalyr? logdaysabsent suspended heldback
	grade_Lzcstmath grade_Lzcstela grade_Lgpatotal grade_Leffmarktotalyr 
	grade_Lzcstmath? grade_Lzcstela? grade_Lgpatotal? grade_Leffmarktotalyr? grade_Lhabmarkfirst5totalyr? 
	grade_Llogdaysabsent grade_Lsuspended grade_Lheldback grade_Lell
	class_Lzcstmath class_Lzcstela class_Lgpatotal class_Leffmarktotalyr 
	class_Lzcstmath? class_Lzcstela? class_Lgpatotal? class_Leffmarktotalyr? 
	class_Llogdaysabsent class_Lsuspended class_Lheldback class_Lell class_size ell cohortsch";
	
#delimit cr

cap file close tables 
file open tables using "../output/table41_interaction.txt", write replace
file write tables "<tab:cog_jackson_interaction>" _n
cap file close tables 

local counter = 1
local modellist = ""
foreach var in hs_dropout eversat satscoretotal hs_gpatotal hs_workeffortgpatotal hs_coopgpatotal  ///
	zcahseescoremath zcahseescoreela hs_dayssuspended hs_logdaysabsent hs_heldback { 
	reg `var' ztva_cog_factor ztva_jackson_factor ztva_cog_jack_interaction ///
		`ctrls'	if grade >= 3 & grade <= 5, cluster(hs_cdscode)
	estimate store model`counter'
	local modellist "`modellist' model`counter'"
	local ++counter
}

estout `modellist' using ../output/table41_interaction.txt, style(tab) append cells(b(fmt(%6.3f) star) se) ///
	stats(N r2, labels(none) fmt(%9.0f %6.3f)) nonumbers nolabel mlabels(none) varlabels(none) collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) varwidth(0) keep(ztva_cog_factor ztva_jackson_factor ztva_cog_jack_interaction)

**********************************************************
*** TABLES: Combine Multiple Hypothesis Testing Tables ***
**********************************************************
* Families of outcomes
import delimited using ../paper/table46c_clusterhsch_rwfam.txt, delim(tab)  clear
drop tabva_clusterhssch_rwfam
gen n=_n
keep if mod(n,4)==0
replace n=n-.5 if n==4
replace n=n+1.5 if n==8
replace n=n+3.5 if n==12
save ../data/lausd_data/temp/rwfam, replace

import delimited using ../paper/table46a_clusterhsch_wyfam.txt, delim(tab)  clear
drop tabva_wy_clusterhssch
gen n=_n
append using ../data/lausd_data/temp/rwfam
sort n
drop n

cap drop matrix out
mkmat _all , matrix(out) 
cap file close tables 

file open tables using "../paper/table46e_clusterhsch_wyrwfam.txt", write replace
file write tables "<tab:va_clusterhssch_rwywfam>" _n
cap file close tables
estout  matrix(out, fmt(%6.3f))  using ../paper/table46e_clusterhsch_wyrwfam.txt, ///
	style(tab) append nonumbers nolabel mlabels(none) varlabels(none) collabels(none)

* All outcomes 	
import delimited using ../paper/table46d_clusterhsch_rwall.txt, delim(tab)  clear
drop tabva_clusterhssch_rwall
gen n=_n
keep if mod(n,4)==0
replace n=n-.5 if n==4
replace n=n+1.5 if n==8
replace n=n+3.5 if n==12
save ../data/lausd_data/temp/rwfam, replace

import delimited using ../paper/table46b_clusterhsch_wyall.txt, delim(tab)  clear
drop tabva_clusterhsch_wyall
gen n=_n
append using ../data/lausd_data/temp/rwfam
sort n
drop n

cap drop matrix out
mkmat _all , matrix(out) 
cap file close tables 
file open tables using "../paper/table46f_clusterhsch_wyrwall.txt", write replace
file write tables "<tab:va_clusterhssch_rwywall>" _n
cap file close tables
estout  matrix(out, fmt(%6.3f))  using ../paper/table46f_clusterhsch_wyrwall.txt, ///
	style(tab) append nonumbers nolabel mlabels(none) varlabels(none) collabels(none)

* Indices	
import delimited using ../paper/table47alt_indexoutcomes_rw.txt, delim(tab)  clear
drop tabva_indexoutcomes_altrw
gen n=_n
keep if mod(n,4)==0
replace n=n-.5 if n==4
replace n=n+1.5 if n==8
replace n=n+3.5 if n==12
save ../data/lausd_data/temp/rwfam, replace

import delimited using ../paper/table48_indexoutcomes_wy.txt, delim(tab)  clear
drop tabva_indexoutcomes_wy
gen n=_n
append using ../data/lausd_data/temp/rwfam
sort n
drop n

cap drop matrix out
mkmat _all , matrix(out) 
cap file close tables 

file open tables using "../paper/table49_indexout_wyrwall.txt", write replace
file write tables "<tab:va_indexout_rwywall>" _n
cap file close tables
estout  matrix(out, fmt(%6.3f))  using ../paper/table49_indexout_wyrwall.txt, ///
	style(tab) append nonumbers nolabel mlabels(none) varlabels(none) collabels(none)


**********************************************************
*** TABLES: G-Formula ***
**********************************************************
forvalues i=1/11{
    import delimited using ../output/table67_gformula_all`i'_v2.txt, delim(tab)  clear
	drop tabva_gformula
	gen n=_n
	ren v2 v`i'
	tempfile gtemp
	save "`gtemp'", replace
	if `i'>1{
		use ../data/lausd_data/temp/gformula_allgrades, clear
		merge 1:1 n using "`gtemp'", nogen
	}
	save ../data/lausd_data/temp/gformula_allgrades, replace
}
forvalues i=1/3{
    import delimited using ../output/table68_gformula_early`i'_v2.txt, delim(tab)  clear
	drop tabva_gformula
	gen n=_n
	if `i'==1{
		ren v2 v1
		ren v3 v2
		ren v4 v3
	}
	if `i'==2{
		ren v5 v7
		ren v4 v6
		ren v3 v5
		ren v2 v4
	}
	if `i'==3{
		ren v2 v8
		ren v3 v9
		ren v4 v10
		ren v5 v11
	}
	
	tempfile gtemp
	save "`gtemp'", replace
	if `i'>1{
		use ../data/lausd_data/temp/gformula_earlygrades, clear
		merge 1:1 n using "`gtemp'", nogen
	}
	save ../data/lausd_data/temp/gformula_earlygrades, replace
}
append using ../data/lausd_data/temp/gformula_allgrades

drop n

mkmat _all , matrix(out) 

file open tables using "../paper/table67_gformula_combined.txt", write replace
file write tables "<tab:va_gformula_combined>" _n
cap file close tables
estout  matrix(out, fmt(%6.3f))  using ../paper/table67_gformula_combined.txt, ///
	style(tab) append nonumbers nolabel mlabels(none) varlabels(none) collabels(none)





cap log close
